-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// The defined CLI settings actions for Vela. | ||
// | ||
// Usage: | ||
// | ||
// import "github.com/go-vela/cli/action/settings" | ||
package settings |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package settings | ||
|
||
// Config represents the configuration necessary | ||
// to perform settings related requests with Vela. | ||
type Config struct { | ||
Action string | ||
Compiler | ||
Queue | ||
RepoAllowlist *[]string | ||
ScheduleAllowlist *[]string | ||
Output string | ||
} | ||
|
||
type Compiler struct { | ||
CloneImage *string | ||
TemplateDepth *int | ||
StarlarkExecLimit *uint64 | ||
} | ||
|
||
type Queue struct { | ||
Routes *[]string | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package settings | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/go-vela/cli/internal/output" | ||
"github.com/go-vela/sdk-go/vela" | ||
"github.com/go-vela/server/api/types/settings" | ||
Check failure on line 10 in action/settings/update.go GitHub Actions / build (windows)
Check failure on line 10 in action/settings/update.go GitHub Actions / build (darwin)
Check failure on line 10 in action/settings/update.go GitHub Actions / test
Check failure on line 10 in action/settings/update.go GitHub Actions / test
Check failure on line 10 in action/settings/update.go GitHub Actions / build (linux)
Check failure on line 10 in action/settings/update.go GitHub Actions / build (darwin)
Check failure on line 10 in action/settings/update.go GitHub Actions / build (windows)
|
||
) | ||
|
||
// Update modifies settings based off the provided configuration. | ||
func (c *Config) Update(client *vela.Client) error { | ||
logrus.Debug("executing update for settings configuration") | ||
|
||
// create the settings object | ||
s := &settings.Platform{ | ||
Queue: &settings.Queue{ | ||
Routes: c.Queue.Routes, | ||
}, | ||
Compiler: &settings.Compiler{ | ||
CloneImage: c.Compiler.CloneImage, | ||
TemplateDepth: c.Compiler.TemplateDepth, | ||
StarlarkExecLimit: c.Compiler.StarlarkExecLimit, | ||
}, | ||
RepoAllowlist: c.RepoAllowlist, | ||
ScheduleAllowlist: c.ScheduleAllowlist, | ||
} | ||
|
||
logrus.Trace("updating settings") | ||
|
||
// send API call to modify settings | ||
s_, _, err := client.Admin.Settings.Update(s) | ||
Check failure on line 34 in action/settings/update.go GitHub Actions / golangci[golangci] action/settings/update.go#L34
Raw output
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
// handle the output based off the provided configuration | ||
switch c.Output { | ||
case output.DriverDump: | ||
// output in dump format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump | ||
return output.Dump(s_) | ||
case output.DriverJSON: | ||
// output in JSON format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON | ||
return output.JSON(s_) | ||
case output.DriverSpew: | ||
// output in spew format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew | ||
return output.Spew(s_) | ||
case output.DriverYAML: | ||
// output in YAML format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML | ||
return output.YAML(s_) | ||
default: | ||
// output in stdout format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout | ||
return output.Stdout(s_) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package settings | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/go-vela/sdk-go/vela" | ||
"github.com/go-vela/server/mock/server" | ||
) | ||
|
||
func TestSettings_Config_Update(t *testing.T) { | ||
// setup test server | ||
s := httptest.NewServer(server.FakeHandler()) | ||
|
||
// create a vela client | ||
client, err := vela.NewClient(s.URL, "vela", nil) | ||
if err != nil { | ||
t.Errorf("unable to create client: %v", err) | ||
} | ||
|
||
// setup tests | ||
tests := []struct { | ||
failure bool | ||
config *Config | ||
}{ | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Action: "update", | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Action: "update", | ||
Output: "dump", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Action: "update", | ||
Output: "json", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Action: "update", | ||
Output: "spew", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Action: "update", | ||
Output: "yaml", | ||
}, | ||
}, | ||
{ | ||
failure: true, | ||
config: &Config{ | ||
Action: "update", | ||
Output: "", | ||
}, | ||
}, | ||
} | ||
|
||
// run tests | ||
for _, test := range tests { | ||
err := test.config.Update(client) | ||
|
||
if test.failure { | ||
if err == nil { | ||
t.Errorf("Update should have returned err") | ||
} | ||
|
||
continue | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("Update returned err: %v", err) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package settings | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Validate verifies the configuration provided. | ||
func (c *Config) Validate() error { | ||
logrus.Debug("validating settings configuration") | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package settings | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSettings_Config_Validate(t *testing.T) { | ||
// setup tests | ||
tests := []struct { | ||
failure bool | ||
config *Config | ||
}{ | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Action: "add", | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: true, | ||
config: &Config{ | ||
Action: "add", | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Action: "add", | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: true, | ||
config: &Config{ | ||
Action: "add", | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: true, | ||
config: &Config{ | ||
Action: "add", | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Action: "view", | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: true, | ||
config: &Config{ | ||
Action: "view", | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Action: "get", | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Action: "update", | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: true, | ||
config: &Config{ | ||
Action: "update", | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: true, | ||
config: &Config{ | ||
Action: "update", | ||
Output: "", | ||
}, | ||
}, | ||
} | ||
|
||
// run tests | ||
for _, test := range tests { | ||
err := test.config.Validate() | ||
|
||
if test.failure { | ||
if err == nil { | ||
t.Errorf("Validate should have returned err") | ||
} | ||
|
||
continue | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("Validate returned err: %v", err) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package settings | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/go-vela/cli/internal/output" | ||
"github.com/go-vela/sdk-go/vela" | ||
) | ||
|
||
// View inspects settings based off the provided configuration. | ||
func (c *Config) View(client *vela.Client) error { | ||
logrus.Debug("executing view for settings configuration") | ||
|
||
logrus.Trace("inspecting settings") | ||
|
||
response, _, err := client.Admin.Settings.Get() | ||
Check failure on line 20 in action/settings/view.go GitHub Actions / golangci[golangci] action/settings/view.go#L20
Raw output
|
||
if err != nil { | ||
return fmt.Errorf("unable to retrieve settings: %w", err) | ||
} | ||
|
||
// handle the output based off the provided configuration | ||
switch c.Output { | ||
case output.DriverDump: | ||
// output in dump format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump | ||
return output.Dump(response) | ||
case output.DriverJSON: | ||
// output in JSON format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON | ||
return output.JSON(response) | ||
case output.DriverSpew: | ||
// output in spew format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew | ||
return output.Spew(response) | ||
case output.DriverYAML: | ||
// output in YAML format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML | ||
return output.YAML(response) | ||
default: | ||
// output in stdout format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout | ||
return output.Stdout(response) | ||
} | ||
} |