-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: replace global state with bootstrap (#17)
ref: move i18n into new internal package translate (#17) ref: define a parameter set for root command (#17) ref: replace init with bootstrap (#17) ref: remove global state from widget command (#17)
- Loading branch information
1 parent
dfdcdd0
commit d7800e0
Showing
6 changed files
with
154 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/snivilised/cobrass/src/assistant" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type bootstrap struct { | ||
container *assistant.CobraContainer | ||
} | ||
|
||
func (b *bootstrap) execute() { | ||
b.container = assistant.NewCobraContainer( | ||
&cobra.Command{ | ||
Use: "main", | ||
Short: "A brief description of your application", | ||
Long: `A longer description that spans multiple lines and likely contains | ||
examples and usage of using your application. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
Version: fmt.Sprintf("'%v'", Version), | ||
// Uncomment the following line if your bare application | ||
// has an action associated with it: | ||
// Run: func(cmd *cobra.Command, args []string) { }, | ||
}, | ||
) | ||
|
||
setupRootCommand(b.container) | ||
BuildWidgetCommand(b.container) | ||
|
||
configure(b.container) | ||
|
||
root := b.container.Root() | ||
|
||
err := root.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func configure(container *assistant.CobraContainer) { | ||
// This is the functionality previously defined in initConfig, which was | ||
// invoked as a result of it be passed into cobra.OnInitialize(). This | ||
// approach was abandoned due to its reliance on global state and the init() | ||
// function which is an anti-pattern. | ||
// | ||
|
||
// initConfig reads in config file and ENV variables if set. | ||
paramSet := container.MustGetParamSet("root-ps").(*assistant.ParamSet[RootParameterSet]) | ||
|
||
if paramSet.Native.ConfigFile != "" { | ||
// Use config file from the flag. | ||
viper.SetConfigFile(paramSet.Native.ConfigFile) | ||
} else { | ||
// Find home directory. | ||
home, err := os.UserHomeDir() | ||
cobra.CheckErr(err) | ||
|
||
// Search config in home directory with name ".arcadia" (without extension). | ||
// NB: 'arcadia' should be renamed as appropriate | ||
// | ||
viper.AddConfigPath(home) | ||
viper.SetConfigType("yaml") | ||
viper.SetConfigName(fmt.Sprintf(".%v", ApplicationName)) | ||
} | ||
|
||
viper.AutomaticEnv() // read in environment variables that match | ||
|
||
// If a config file is found, read it in. | ||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package translate | ||
|
||
// import ( | ||
// "encoding/json" | ||
|
2 changes: 1 addition & 1 deletion
2
src/app/main/i18n_test.go → src/internal/translate/i18n_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main_test | ||
package translate_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package translate_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestTranslate(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Translate Suite") | ||
} |