Skip to content

Commit

Permalink
Change from urfave/cli to spf13/cobra. Fixes #89
Browse files Browse the repository at this point in the history
  • Loading branch information
tooolbox committed May 27, 2020
1 parent 16b5dfc commit b2de448
Show file tree
Hide file tree
Showing 7 changed files with 490 additions and 266 deletions.
242 changes: 0 additions & 242 deletions cmd/app.go

This file was deleted.

50 changes: 50 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd

import (
"fmt"
"os"
"strings"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string

var RootCmd = &cobra.Command{
Use: "kala",
Short: "Modern job scheduler",
Long: `See https://github.com/ajvb/kala for documentation.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return viper.BindPFlags(cmd.Flags())
},
}

func init() {
cobra.OnInitialize(initViper)
RootCmd.PersistentFlags().StringVar(&cfgFile, "kala", "", "config file (default is $HOME/kala.yaml)")
}

func initViper() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

viper.AddConfigPath(home)
viper.SetConfigName("kala")
}

viper.SetEnvPrefix("kala")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
38 changes: 38 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"fmt"
"log"

"github.com/ajvb/kala/job"
"github.com/spf13/cobra"
)

var runCommandCmd = &cobra.Command{
Use: "run",
Short: "run a job",
Long: `runs the specific job immediately and returns the result`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("Must include a command")
} else if len(args) > 1 {
log.Fatal("Must only include a command")
}

j := &job.Job{
Command: args[0],
}

out, err := j.RunCmd()
if err != nil {
log.Fatalf("Command Failed with err: %s", err)
} else {
fmt.Println("Command Succeeded!")
fmt.Println("Output: ", out)
}
},
}

func init() {
RootCmd.AddCommand(runCommandCmd)
}
Loading

0 comments on commit b2de448

Please sign in to comment.