github.com/omnigres/cli@v0.1.4/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/charmbracelet/log"
     5  	"github.com/spf13/cobra"
     6  
     7  	"os"
     8  	"path/filepath"
     9  )
    10  
    11  // rootCmd represents the base command when called without any subcommands
    12  var rootCmd = &cobra.Command{
    13  	Use:   "omnigres",
    14  	Short: "Omnigres CLI",
    15  	Long:  `Omnigres CLI toolkit allows easy creation and management of Omnigres applications and orbs.`,
    16  	PersistentPreRun: func(cmd *cobra.Command, args []string) {
    17  		if verbose {
    18  			log.SetLevel(log.DebugLevel)
    19  			log.Debug("Verbose mode enabled")
    20  		}
    21  	},
    22  }
    23  
    24  func Execute() {
    25  	err := rootCmd.Execute()
    26  	if err != nil {
    27  		os.Exit(1)
    28  	}
    29  }
    30  
    31  var workspace string
    32  var verbose bool
    33  
    34  func findOmnigresDir(dir string) (string, error) {
    35  	if _, err := os.Stat(filepath.Join(dir, "omnigres.yaml")); err == nil {
    36  		return dir, nil
    37  	}
    38  
    39  	parent := filepath.Dir(dir)
    40  	if parent == dir {
    41  		// Reached the root, return original working directory
    42  		return os.Getwd()
    43  	}
    44  	return findOmnigresDir(parent)
    45  }
    46  
    47  func init() {
    48  	cwd, err := os.Getwd()
    49  	if err != nil {
    50  		log.Fatal(err)
    51  	}
    52  
    53  	omnigresDir, err := findOmnigresDir(cwd)
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  	rootCmd.PersistentFlags().StringVarP(&workspace, "workspace", "w", omnigresDir, "path to workspace")
    58  	rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "display debug messages")
    59  }