oh-my-posh/src/cli/init.go

67 lines
1.3 KiB
Go
Raw Normal View History

package cli
2022-03-12 13:04:08 -08:00
import (
"fmt"
"oh-my-posh/environment"
2022-03-21 23:41:36 -07:00
"oh-my-posh/shell"
2022-03-12 13:04:08 -08:00
"github.com/spf13/cobra"
)
var (
print bool
strict bool
2022-03-12 13:04:08 -08:00
initCmd = &cobra.Command{
2022-04-10 10:33:13 -07:00
Use: "init [bash|zsh|fish|powershell|pwsh|cmd|nu] --config ~/.mytheme.omp.json",
Short: "Initialize your shell and config",
Long: `Initialize your shell and config.
See the documentation to initialize your shell: https://ohmyposh.dev/docs/installation/prompt.`,
2022-03-12 13:04:08 -08:00
ValidArgs: []string{
"bash",
"zsh",
"fish",
"powershell",
"pwsh",
"cmd",
2022-04-10 10:33:13 -07:00
"nu",
2022-03-12 13:04:08 -08:00
},
Args: NoArgsOrOneValidArg,
2022-03-12 13:04:08 -08:00
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
_ = cmd.Help()
return
}
2022-03-12 13:04:08 -08:00
runInit(args[0])
},
}
)
func init() { // nolint:gochecknoinits
initCmd.Flags().BoolVarP(&print, "print", "p", false, "print the init script")
initCmd.Flags().BoolVarP(&strict, "strict", "s", false, "run in strict mode")
2022-03-12 13:04:08 -08:00
_ = initCmd.MarkPersistentFlagRequired("config")
2022-03-25 11:03:37 -07:00
rootCmd.AddCommand(initCmd)
2022-03-12 13:04:08 -08:00
}
2022-03-21 23:41:36 -07:00
func runInit(shellName string) {
2022-03-12 13:04:08 -08:00
env := &environment.ShellEnvironment{
Version: cliVersion,
2022-03-12 13:04:08 -08:00
CmdFlags: &environment.Flags{
2022-03-21 23:41:36 -07:00
Shell: shellName,
2022-03-12 13:04:08 -08:00
Config: config,
Strict: strict,
2022-03-12 13:04:08 -08:00
},
}
env.Init()
2022-03-12 13:04:08 -08:00
defer env.Close()
if print {
2022-03-21 23:41:36 -07:00
init := shell.PrintInit(env)
2022-03-12 13:04:08 -08:00
fmt.Print(init)
return
}
2022-03-21 23:41:36 -07:00
init := shell.Init(env)
2022-03-12 13:04:08 -08:00
fmt.Print(init)
}