Skip to content

Commit

Permalink
Add basic-cli example
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottKGregory committed Feb 28, 2021
1 parent 4c26156 commit d0272e2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
46 changes: 46 additions & 0 deletions examples/basic-cli/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"fmt"

"github.com/rs/zerolog"
"github.com/scottkgregory/mamba"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type AppConfig struct {
Messages Messages `config:","`
Snakes []string `config:"[\"adder\"],A list of snakes. Hsssss!"`
*Embedded `config:","`
}

type Messages struct {
Greeting string `config:"Hello there!,The greating to use"`
}

type Embedded struct {
Farewell string `config:"Goodbye!,The farewell to use"`
}

var rootCmd = &cobra.Command{
Use: "basic-cli",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(viper.GetString("messages.greeting"))
fmt.Println("These are all the snakes that exist in the world:")
for _, s := range viper.GetStringSlice("snakes") {
fmt.Printf(" - %s\n", s)
}
fmt.Println(viper.GetString("embedded.farewell"))
},
}

func Execute() {
cobra.CheckErr(rootCmd.Execute())
}

func init() {
mamba.Bind(AppConfig{}, rootCmd, &mamba.Options{LogLevel: zerolog.TraceLevel})
}
7 changes: 7 additions & 0 deletions examples/basic-cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/scottkgregory/mamba/examples/basic-cli/cmd"

func main() {
cmd.Execute()
}

0 comments on commit d0272e2

Please sign in to comment.