main API

main

package

API reference for the main package.

F
function

main

examples/v1/app-base/main.go:12-38
func main()

{
	// Here we create a new Vanilla OS application
	myApp, err := app.NewApp(types.AppOptions{
		RDNN:    "com.vanillaos.batsignal",
		Name:    "BatSignal",
		Version: "1.0.0",
	})
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Then log a welcome message
	myApp.Log.Term.Info().Msgf("Welcome to %s (%s)!", myApp.Name, myApp.Version)
	myApp.Log.Term.Info().Msg("You just called Batman!")

	// Using contextual logging
	rootCtx := logs.NewLogContext("Main", nil)
	stepCtx := logs.NewLogContext("Init", rootCtx)

	// Logging with context
	myApp.Log.InfoCtx(rootCtx, "Starting example")
	myApp.Log.ErrorCtx(stepCtx, "This is just a test error")

	// And finally, log a message to the file logger
	myApp.Log.File.Info().Msg("Batman reached the file logger")
}
S
struct

RootCmd

examples/v1/app-cli/main.go:15-19
type RootCmd struct

Fields

Name Type Description
Poll PollCmd cmd:"poll" help:"Ask the user preferred hero"
Man ManCmd cmd:"man" help:"Generate man page"
S
struct

ManCmd

examples/v1/app-cli/main.go:21-23
type ManCmd struct

Methods

Run
Method

Returns

error
func (*ManCmd) Run() error
{
	man, err := cli.GenerateManPage(&RootCmd{}, func(key string) string {
		return key
	})
	if err != nil {
		return err
	}
	fmt.Print(man)
	return nil
}
S
struct

PollCmd

examples/v1/app-cli/main.go:36-38
type PollCmd struct

Methods

Run
Method

Returns

error
func (*PollCmd) Run() error
{
	fmt.Printf("Welcome to %s (%s)!\n", myApp.Name, myApp.Version)
	hero, err := myApp.CLI.SelectOption(
		"What is your preferred hero?",
		[]string{"Batman", "Ironman", "Spiderman", "Robin", "None"},
	)
	if err != nil {
		fmt.Println(err)
		return err
	}

	switch hero {
	case "Batman":
		myApp.Log.Term.Info().Msg("I am Batman!")
	case "Ironman":
		myApp.Log.Term.Info().Msg("Yeah Ironman is cool!")
	case "Spiderman":
		myApp.Log.Term.Info().Msg("Spiderman is ok.")
	case "Robin":
		myApp.Log.Term.Info().Msg("Nobody likes Robin.")
	case "None":
		// Let's ask if they want to pick an unlisted hero
		confirm, err := myApp.CLI.ConfirmAction(
			"Do you want to pick an unlisted hero?",
			"Y", "n",
			true,
		)
		if err != nil {
			fmt.Println(err)
			return err
		}

		if confirm {
			hero, err := myApp.CLI.PromptText(
				"Enter your preferred hero",
				"Batman",
			)
			if err != nil {
				fmt.Println(err)
				return err
			}

			// Let's simulate a spinner while we process the input
			spinner := myApp.CLI.StartSpinner("Looking for your hero...")
			time.Sleep(3 * time.Second)
			spinner.Stop()
			myApp.Log.Term.Info().Msgf("You picked %s", hero)

			// Showing a progressbar now
			bar := myApp.CLI.StartProgressBar("Preparing your hero...", 100)
			for i := 0; i <= 100; i++ {
				bar.Increment(1)
				time.Sleep(50 * time.Millisecond)
			}

			myApp.Log.Term.Info().Msgf("Here is %s!", hero)
		}
	}

	return nil
}
F
function

main

examples/v1/app-cli/main.go:102-124
func main()

{
	// Here we create a new Vanilla OS application
	var err error
	myApp, err = app.NewApp(types.AppOptions{
		RDNN:    "com.vanillaos.batpoll",
		Name:    "BatPoll",
		Version: "1.0.0",
	})
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Here we add a CLI to the application
	err = myApp.WithCLI(&RootCmd{})
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// And finally, run the CLI
	myApp.CLI.Execute()
}