Skip to content

Commit

Permalink
add config profile support
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman committed Jul 29, 2024
1 parent ac88e09 commit 13b7978
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
12 changes: 6 additions & 6 deletions development.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
)

const (
ProfileCPU Profile = "cpu"
ProfileMem Profile = "mem"
ProfilingDisabled Profile = "none"
ProfileCPU ProfileResource = "cpu"
ProfileMem ProfileResource = "mem"
ProfilingDisabled ProfileResource = "none"
)

type Profile string
type ProfileResource string

type DevelopmentConfig struct {
Profile Profile `yaml:"profile" json:"profile" mapstructure:"profile"`
Profile ProfileResource `yaml:"profile" json:"profile" mapstructure:"profile"`
}

func (d *DevelopmentConfig) DescribeFields(set fangs.FieldDescriptionSet) {
Expand All @@ -32,7 +32,7 @@ func (d *DevelopmentConfig) PostLoad() error {
return nil
}

func parseProfile(profile string) Profile {
func parseProfile(profile string) ProfileResource {
switch strings.ToLower(profile) {
case "cpu":
return ProfileCPU
Expand Down
2 changes: 1 addition & 1 deletion development_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func Test_parseProfile(t *testing.T) {
tests := []struct {
name string
profile string
want Profile
want ProfileResource
}{
{
name: "empty",
Expand Down
29 changes: 29 additions & 0 deletions setup_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package clio

import (
"fmt"
"os"
"path/filepath"

"github.com/wagoodman/go-partybus"

"github.com/anchore/fangs"
Expand Down Expand Up @@ -75,6 +79,31 @@ func (c *SetupConfig) WithConfigFinders(finders ...fangs.Finder) *SetupConfig {
return c
}

func (c *SetupConfig) WithConfigProfile() *SetupConfig {
c.FangsConfig = c.FangsConfig.WithProfileEnvVar()

Check failure on line 83 in setup_config.go

View workflow job for this annotation

GitHub Actions / Unit tests

c.FangsConfig.WithProfileEnvVar undefined (type fangs.Config has no field or method WithProfileEnvVar)

Check failure on line 83 in setup_config.go

View workflow job for this annotation

GitHub Actions / Static analysis

c.FangsConfig.WithProfileEnvVar undefined (type fangs.Config has no field or method WithProfileEnvVar)

Check failure on line 83 in setup_config.go

View workflow job for this annotation

GitHub Actions / Static analysis

c.FangsConfig.WithProfileEnvVar undefined (type fangs.Config has no field or method WithProfileEnvVar)
c.FangsConfig.Finders = append([]fangs.Finder{func(cfg fangs.Config) ([]string, error) {

Check failure on line 84 in setup_config.go

View workflow job for this annotation

GitHub Actions / Unit tests

cannot use func(cfg fangs.Config) ([]string, error) {…} (value of type func(cfg fangs.Config) ([]string, error)) as fangs.Finder value in array or slice literal

Check failure on line 84 in setup_config.go

View workflow job for this annotation

GitHub Actions / Static analysis

cannot use func(cfg fangs.Config) ([]string, error) {…} (value of type func(cfg fangs.Config) ([]string, error)) as fangs.Finder value in array or slice literal

Check failure on line 84 in setup_config.go

View workflow job for this annotation

GitHub Actions / Static analysis

cannot use func(cfg fangs.Config) ([]string, error) {…} (value of type func(cfg fangs.Config) ([]string, error)) as fangs.Finder value in array or slice literal
if cfg.Profile == "" {

Check failure on line 85 in setup_config.go

View workflow job for this annotation

GitHub Actions / Unit tests

cfg.Profile undefined (type fangs.Config has no field or method Profile)

Check failure on line 85 in setup_config.go

View workflow job for this annotation

GitHub Actions / Static analysis

cfg.Profile undefined (type fangs.Config has no field or method Profile)

Check failure on line 85 in setup_config.go

View workflow job for this annotation

GitHub Actions / Static analysis

cfg.Profile undefined (type fangs.Config has no field or method Profile)
return nil, nil
}
file := filepath.Join("."+cfg.AppName, cfg.Profile+".yaml")

Check failure on line 88 in setup_config.go

View workflow job for this annotation

GitHub Actions / Unit tests

cfg.Profile undefined (type fangs.Config has no field or method Profile)

Check failure on line 88 in setup_config.go

View workflow job for this annotation

GitHub Actions / Static analysis

cfg.Profile undefined (type fangs.Config has no field or method Profile)

// if path does not exist, return error
if _, err := os.Stat(file); os.IsNotExist(err) {
return nil, fmt.Errorf("config file not found: %s", file)
}

return []string{file}, nil
}}, c.FangsConfig.Finders...)

c.Initializers = append(c.Initializers, func(state *State) error {
if c.FangsConfig.Profile != "" {

Check failure on line 99 in setup_config.go

View workflow job for this annotation

GitHub Actions / Unit tests

c.FangsConfig.Profile undefined (type fangs.Config has no field or method Profile)

Check failure on line 99 in setup_config.go

View workflow job for this annotation

GitHub Actions / Static analysis

c.FangsConfig.Profile undefined (type fangs.Config has no field or method Profile)
state.Logger.Infof("Using config profile: %q", c.FangsConfig.Profile)

Check failure on line 100 in setup_config.go

View workflow job for this annotation

GitHub Actions / Unit tests

c.FangsConfig.Profile undefined (type fangs.Config has no field or method Profile)

Check failure on line 100 in setup_config.go

View workflow job for this annotation

GitHub Actions / Static analysis

c.FangsConfig.Profile undefined (type fangs.Config has no field or method Profile) (typecheck)
}
return nil
})
return c
}

func (c *SetupConfig) WithDevelopmentConfig(cfg DevelopmentConfig) *SetupConfig {
c.DefaultDevelopmentConfig = &cfg
return c
Expand Down

0 comments on commit 13b7978

Please sign in to comment.