cloudflare/cfssl_trust
Publicmirrored fromhttps://github.com/cloudflare/cfssl_trustAvailable
cli/root.go
121lines · modecode
| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | |
| 9 | "github.com/cloudflare/cfssl/log" |
| 10 | "github.com/cloudflare/cfssl_trust/config" |
| 11 | "github.com/cloudflare/cfssl_trust/release" |
| 12 | "github.com/spf13/cobra" |
| 13 | "github.com/spf13/viper" |
| 14 | ) |
| 15 | |
| 16 | var ( |
| 17 | cfgFile string |
| 18 | dbFile string |
| 19 | bundle string |
| 20 | bundleRelease string |
| 21 | ) |
| 22 | |
| 23 | func root(cmd *cobra.Command, args []string) { |
| 24 | err := cmd.Help() |
| 25 | if err != nil { |
| 26 | os.Exit(1) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | var configLocations = []string{ |
| 31 | "/etc/cfssl", |
| 32 | "/usr/local/cfssl", |
| 33 | filepath.Join(config.GoPath(), "src", "github.com", "cloudflare", "cfssl_trust"), |
| 34 | } |
| 35 | |
| 36 | var rootCmd = &cobra.Command{ |
| 37 | Use: "cfssl-trust", |
| 38 | Short: "Manage a trust database for root and intermediate bundles.", |
| 39 | Long: ``, |
| 40 | Run: root, |
| 41 | } |
| 42 | |
| 43 | // Execute runs the cfssl-trust binary |
| 44 | func Execute() { |
| 45 | if err := rootCmd.Execute(); err != nil { |
| 46 | fmt.Println(err) |
| 47 | os.Exit(-1) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // If err isn't nil, this should rollback the transaction. If err is |
| 52 | // nil, it should commit the transaction. Finally, it should close the |
| 53 | // database. |
| 54 | func cleanup(tx *sql.Tx, db *sql.DB, err error) { |
| 55 | if tx != nil { |
| 56 | if err != nil { |
| 57 | err = tx.Rollback() |
| 58 | if err != nil { |
| 59 | fmt.Fprintf(os.Stderr, "[!] error while rolling back transaction: %s\n", err) |
| 60 | os.Exit(1) |
| 61 | } |
| 62 | } else { |
| 63 | err = tx.Commit() |
| 64 | if err != nil { |
| 65 | fmt.Fprintf(os.Stderr, "[!] error while committing transaction: %s\n", err) |
| 66 | os.Exit(1) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if db != nil { |
| 72 | err = db.Close() |
| 73 | if err != nil { |
| 74 | fmt.Fprintf(os.Stderr, "[!] error while closing database: %s\n", err) |
| 75 | os.Exit(1) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func init() { |
| 81 | cobra.OnInitialize(initConfig) |
| 82 | |
| 83 | rootCmd.PersistentFlags().StringVarP(&bundle, "bundle", "b", "int", "select a bundle (ca or int)") |
| 84 | rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "f", "", "config file (default is /etc/cfssl/cfssl-trust.yaml)") |
| 85 | rootCmd.PersistentFlags().StringVarP(&dbFile, "db", "d", "", "path to trust database") |
| 86 | rootCmd.PersistentFlags().StringVarP(&bundleRelease, "release", "r", "", "select a release") |
| 87 | |
| 88 | viper.BindPFlag("database.path", rootCmd.PersistentFlags().Lookup("db")) |
| 89 | } |
| 90 | |
| 91 | // initConfig reads in config file and ENV variables if set. |
| 92 | func initConfig() { |
| 93 | if cfgFile != "" { // enable ability to specify config file via flag |
| 94 | viper.SetConfigFile(cfgFile) |
| 95 | } else { |
| 96 | viper.SetConfigName("cfssl-trust") // name of config file (without extension) |
| 97 | for _, dir := range configLocations { |
| 98 | viper.AddConfigPath(dir) |
| 99 | } |
| 100 | viper.AddConfigPath(".") |
| 101 | } |
| 102 | |
| 103 | viper.SetEnvPrefix("CFSSL_TRUST") |
| 104 | viper.AutomaticEnv() // read in environment variables that match |
| 105 | |
| 106 | // If a config file is found, read it in. |
| 107 | err := viper.ReadInConfig() |
| 108 | if err == nil { |
| 109 | log.Info("cfssl-trust: loading from config file ", viper.ConfigFileUsed()) |
| 110 | } |
| 111 | |
| 112 | if bundleRelease != "" { |
| 113 | rel, err := release.Parse(bundleRelease) |
| 114 | if err != nil { |
| 115 | fmt.Fprintf(os.Stderr, "[!] Invalid release '%s'.\n", bundleRelease) |
| 116 | fmt.Fprintf(os.Stderr, "\tReason: %s\n", err) |
| 117 | os.Exit(1) |
| 118 | } |
| 119 | fmt.Println("selected release", rel) |
| 120 | } |
| 121 | } |
| 122 | |