Skip to content

Commit

Permalink
- lazily load goal only if required
Browse files Browse the repository at this point in the history
- fix issue with env commands
  • Loading branch information
aaabramov committed Nov 10, 2021
1 parent 9a91e66 commit 913b606
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
3 changes: 3 additions & 0 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var cliCmd = &cobra.Command{
Use: "cli GOAL [--on env]",
Short: "Show CLI for specific goal",
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
loadGoals()
},
Run: func(cmd *cobra.Command, args []string) {
goal := args[0]
if cmd, exists := commands.GetWithEnv(strings.TrimSpace(goal), env); exists {
Expand Down
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var rootCmd = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
loadGoals()
commands.Render()
}
},
Expand All @@ -35,8 +36,6 @@ func Execute() {
}

func init() {
cobra.OnInitialize(loadGoals)

rootCmd.PersistentFlags().StringVarP(&goalFile, "config", "c", "goal.yaml", "goals file to use")
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var runCmd = &cobra.Command{
}
return res, cobra.ShellCompDirectiveNoFileComp
},
PreRun: func(cmd *cobra.Command, args []string) {
loadGoals()
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
commands.Exec(strings.TrimSpace(args[0]), env)
Expand Down
8 changes: 2 additions & 6 deletions lib/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ func (c *Goals) get(name string) (*Goal, bool) {

func (c *Goals) GetWithEnv(name string, env string) (*Goal, bool) {
for _, command := range c.Commands {
if command.Name == name {
if command.Env != "" && env != "" {
return &command, true
} else if command.Env == "" {
return &command, true
}
if command.Name == name && command.Env == env {
return &command, true
}
}
return nil, false
Expand Down

0 comments on commit 913b606

Please sign in to comment.