Skip to content

Commit

Permalink
feat: settings view and update with optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed May 3, 2024
1 parent 9f770cb commit a01afd2
Show file tree
Hide file tree
Showing 13 changed files with 716 additions and 0 deletions.
8 changes: 8 additions & 0 deletions action/settings/doc.go
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
24 changes: 24 additions & 0 deletions action/settings/settings.go
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
}
67 changes: 67 additions & 0 deletions action/settings/update.go
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

View workflow job for this annotation

GitHub Actions / build (windows)

no required module provides package github.com/go-vela/server/api/types/settings; to add it:

Check failure on line 10 in action/settings/update.go

View workflow job for this annotation

GitHub Actions / build (darwin)

no required module provides package github.com/go-vela/server/api/types/settings; to add it:

Check failure on line 10 in action/settings/update.go

View workflow job for this annotation

GitHub Actions / test

no required module provides package github.com/go-vela/server/api/types/settings; to add it:

Check failure on line 10 in action/settings/update.go

View workflow job for this annotation

GitHub Actions / test

no required module provides package github.com/go-vela/server/api/types/settings; to add it:

Check failure on line 10 in action/settings/update.go

View workflow job for this annotation

GitHub Actions / build (linux)

no required module provides package github.com/go-vela/server/api/types/settings; to add it:

Check failure on line 10 in action/settings/update.go

View workflow job for this annotation

GitHub Actions / build (darwin)

no required module provides package github.com/go-vela/server/api/types/settings; to add it:

Check failure on line 10 in action/settings/update.go

View workflow job for this annotation

GitHub Actions / build (windows)

no required module provides package github.com/go-vela/server/api/types/settings; to add it:

Check failure on line 10 in action/settings/update.go

View workflow job for this annotation

GitHub Actions / build (linux)

no required module provides package github.com/go-vela/server/api/types/settings; to add it:
)

// 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

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/settings/update.go#L34

client.Admin.Settings undefined (type *vela.AdminService has no field or method Settings) (typecheck)
Raw output
action/settings/update.go:34:29: client.Admin.Settings undefined (type *vela.AdminService has no field or method Settings) (typecheck)
	s_, _, err := client.Admin.Settings.Update(s)
	                           ^
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_)
}
}
88 changes: 88 additions & 0 deletions action/settings/update_test.go
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)
}
}
}
14 changes: 14 additions & 0 deletions action/settings/validate.go
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
}
110 changes: 110 additions & 0 deletions action/settings/validate_test.go
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)
}
}
}
53 changes: 53 additions & 0 deletions action/settings/view.go
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

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/settings/view.go#L20

client.Admin.Settings undefined (type *vela.AdminService has no field or method Settings) (typecheck)
Raw output
action/settings/view.go:20:35: client.Admin.Settings undefined (type *vela.AdminService has no field or method Settings) (typecheck)
	response, _, err := client.Admin.Settings.Get()
	                                 ^
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)
}
}
Loading

0 comments on commit a01afd2

Please sign in to comment.