You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.2 KiB

  1. /*
  2. Copyright © 2021 Drew Short <warricks@sothr.com>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "fmt"
  16. "github.com/spf13/cobra"
  17. "os"
  18. "github.com/spf13/viper"
  19. )
  20. var configFile string
  21. var remoteURL string
  22. // rootCmd represents the base command when called without any subcommands
  23. var rootCmd = &cobra.Command{
  24. Use: "pinned-package-updater",
  25. Short: "Update pinned packages in Dockerfiles",
  26. Long: `A longer description that spans multiple lines and likely contains
  27. examples and usage of using your application. For example:
  28. TODO: Add longer description with examples
  29. Cobra is a CLI library for Go that empowers applications.
  30. This application is a tool to generate the needed files
  31. to quickly create a Cobra application.`,
  32. // Uncomment the following line if your bare application
  33. // has an action associated with it:
  34. // Run: func(cmd *cobra.Command, args []string) { },
  35. }
  36. // Execute adds all child commands to the root command and sets flags appropriately.
  37. // This is called by main.main(). It only needs to happen once to the rootCmd.
  38. func Execute() {
  39. cobra.CheckErr(rootCmd.Execute())
  40. }
  41. func init() {
  42. cobra.OnInitialize(initConfig)
  43. // Here you will define your flags and configuration settings.
  44. // Cobra supports persistent flags, which, if defined here,
  45. // will be global for your application.
  46. rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is $HOME/.pinned-package-updater.yaml OR ./.pinned-package-updater.yaml)")
  47. rootCmd.PersistentFlags().StringVar(&remoteURL, "remote", "", "remote url for distributed mode (example: https://ppu.example.com/)")
  48. // Cobra also supports local flags, which will only run
  49. // when this action is called directly.
  50. rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
  51. }
  52. // initConfig reads in config file and ENV variables if set.
  53. func initConfig() {
  54. if configFile != "" {
  55. viper.SetConfigFile(configFile)
  56. } else {
  57. // Search config in home directory with name ".pinned-package-updater" (without extension).
  58. homeDirectory, homeDirectoryErr := os.UserHomeDir()
  59. cobra.CheckErr(homeDirectoryErr)
  60. viper.AddConfigPath(homeDirectory)
  61. viper.SetConfigType("yaml")
  62. viper.SetConfigName(".pinned-package-updater")
  63. // Search config in current directory with name ".pinned-package-updater" (without extension).
  64. currentDirectory, currentDirectoryErr := os.Getwd()
  65. cobra.CheckErr(currentDirectoryErr)
  66. viper.AddConfigPath(currentDirectory)
  67. viper.SetConfigType("yaml")
  68. viper.SetConfigName(".pinned-package-updater")
  69. }
  70. viper.AutomaticEnv() // read in environment variables that match
  71. // If a config file is found, read it in.
  72. if err := viper.ReadInConfig(); err == nil {
  73. _, _ = fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
  74. }
  75. }