Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sticky option for --disable-logo #231

Merged
1 commit merged into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions pkg/cli/cli_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ const configFileName = "options.yaml"
type (
Options struct {
Update UpdateOptions `yaml:",omitempty"`
UI UIOptions `yaml:",omitempty"`
}

UpdateOptions struct {
Stream string `yaml:",omitempty"`
Stream OptionalString `yaml:",omitempty"`
}

UIOptions struct {
DisableLogo OptionalBool `yaml:"disable-logo,omitempty"`
}

OptionalString string

// OptionalBool is useful when you want to differentiate between "unset" and "false".
OptionalBool struct {
*bool
}
)

Expand Down Expand Up @@ -122,11 +134,33 @@ func readOptionsFileBytes() (string, []byte, error) {
return path, content, err
}

func OptionOrDefault(given string, defaultValue string) string {
if given == "" {
return defaultValue
func (s OptionalString) OrDefault(defValue string) string {
if s == "" {
return defValue
} else {
return string(s)
}
}

func (b OptionalBool) OrDefault(defValue bool) bool {
if b.bool == nil {
return defValue
} else {
return *b.bool
}
}

func (b *OptionalBool) MarshalYAML() (interface{}, error) {
return b.bool, nil
}

func (b *OptionalBool) UnmarshalYAML(unmarshal func(interface{}) error) error {
var maybeBool bool
if err := unmarshal(&maybeBool); err != nil {
return err
}
return given
b.bool = &maybeBool
return nil
}

func ShouldCheckForUpdate(updateStreamOverride string, defaultUpdateStream string, currVersion string) bool {
Expand Down
32 changes: 18 additions & 14 deletions pkg/cli/klothomain.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ var cfg struct {
setOption map[string]string
}

const defaultDisableLogo = false

var hadWarnings = atomic.NewBool(false)
var hadErrors = atomic.NewBool(false)

Expand Down Expand Up @@ -84,7 +86,7 @@ func (km KlothoMain) Main() {
flags.StringVar(&cfg.appName, "app", "", "Application name")
flags.StringVarP(&cfg.provider, "provider", "p", "", fmt.Sprintf("Provider to compile to. Supported: %v", "aws"))
flags.BoolVar(&cfg.strict, "strict", false, "Fail the compilation on warnings")
flags.BoolVar(&cfg.disableLogo, "disable-logo", false, "Disable printing the Klotho logo")
flags.BoolVar(&cfg.disableLogo, "disable-logo", defaultDisableLogo, "Disable printing the Klotho logo")
flags.BoolVar(&cfg.uploadSource, "upload-source", false, "Upload the compressed source folder for debugging")
flags.BoolVar(&cfg.internalDebug, "internalDebug", false, "Enable debugging for compiler")
flags.BoolVar(&cfg.version, "version", false, "Print the version")
Expand Down Expand Up @@ -151,9 +153,21 @@ func readConfig(args []string) (appCfg config.Application, err error) {
}

func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
// Save any config options. This should go before anything else, so that it always takes effect before any code
// that uses it (for example, we should save an update.stream option before we use it below to perform the update).
err = SetOptions(cfg.setOption)
if err != nil {
return err
}
options, err := ReadOptions()
if err != nil {
return err
}

showLogo := !(options.UI.DisableLogo.OrDefault(defaultDisableLogo) || cfg.disableLogo)
// color.NoColor is set if we're not a terminal that
// supports color
if !color.NoColor && !cfg.disableLogo {
if !color.NoColor && showLogo {
color.New(color.FgHiGreen).Println(Logo)
fmt.Println()
}
Expand Down Expand Up @@ -199,17 +213,7 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
}
defer analyticsClient.PanicHandler(&err, errHandler)

// Save any config options. This should go before anything else, so that it always takes effect before any code
// that uses it (for example, we should save an update.stream option before we use it below to perform the update).
err = SetOptions(cfg.setOption)
if err != nil {
return err
}
options, err := ReadOptions()
if err != nil {
return err
}
updateStream := OptionOrDefault(options.Update.Stream, km.DefaultUpdateStream)
updateStream := options.Update.Stream.OrDefault(km.DefaultUpdateStream)
analyticsClient.Properties["updateStream"] = updateStream

if cfg.version {
Expand Down Expand Up @@ -240,7 +244,7 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
return nil
}

if ShouldCheckForUpdate(options.Update.Stream, km.DefaultUpdateStream, km.Version) {
if ShouldCheckForUpdate(updateStream, km.DefaultUpdateStream, km.Version) {
// check daily for new updates and notify users if found
needsUpdate, err := klothoUpdater.CheckUpdate(km.Version)
if err != nil {
Expand Down