github.com/pojntfx/hydrapp/hydrapp@v0.0.0-20240516002902-d08759d6ca9f/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/spf13/cobra"
     9  	"github.com/spf13/viper"
    10  )
    11  
    12  const (
    13  	secretsFlag = "secrets"
    14  )
    15  
    16  var rootCmd = &cobra.Command{
    17  	Use:   "hydrapp",
    18  	Short: "Build apps that run everywhere with Go",
    19  	Long: `Build apps that run everywhere with Go and a browser engine of your choice (Chrome, Firefox, Epiphany or Android WebView).
    20  Find more information at:
    21  https://github.com/pojntfx/hydrapp`,
    22  	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    23  		viper.SetEnvPrefix("hydrapp")
    24  		viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
    25  
    26  		if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
    27  			return err
    28  		}
    29  
    30  		return nil
    31  	},
    32  }
    33  
    34  func Execute() error {
    35  	dataHomeDir := os.Getenv("XDG_DATA_HOME")
    36  	if strings.TrimSpace(dataHomeDir) == "" {
    37  		userHomeDir, err := os.UserHomeDir()
    38  		if err != nil {
    39  			panic(err)
    40  		}
    41  
    42  		dataHomeDir = filepath.Join(userHomeDir, ".local", "share")
    43  	}
    44  
    45  	rootCmd.PersistentFlags().String(secretsFlag, filepath.Join(dataHomeDir, "hydrapp", "secrets.yaml"), "Secrets file to use")
    46  
    47  	if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
    48  		return err
    49  	}
    50  
    51  	viper.AutomaticEnv()
    52  
    53  	return rootCmd.Execute()
    54  }