feat: initialize shell with default remote config

This commit is contained in:
Jan De Dobbeleer 2022-03-16 08:47:39 +01:00 committed by Jan De Dobbeleer
parent d11c39b620
commit 75e90329f4
7 changed files with 33 additions and 25 deletions

View file

@ -35,6 +35,7 @@ Exports the ~/myconfig.omp.json config file to toml and writes the result to yo
A backup of the current config can be found at ~/myconfig.omp.json.bak.`, A backup of the current config can be found at ~/myconfig.omp.json.bak.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
env := &environment.ShellEnvironment{ env := &environment.ShellEnvironment{
Version: cliVersion,
CmdFlags: &environment.Flags{ CmdFlags: &environment.Flags{
Config: config, Config: config,
}, },

View file

@ -41,6 +41,7 @@ You can tweak the output by using additional flags:
- background-color: the background color of the image`, - background-color: the background color of the image`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
env := &environment.ShellEnvironment{ env := &environment.ShellEnvironment{
Version: cliVersion,
CmdFlags: &environment.Flags{ CmdFlags: &environment.Flags{
Config: config, Config: config,
Shell: "shell", Shell: "shell",

View file

@ -29,7 +29,9 @@ This command is used to get the value of the following variables:
}, },
Args: cobra.OnlyValidArgs, Args: cobra.OnlyValidArgs,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
env := &environment.ShellEnvironment{} env := &environment.ShellEnvironment{
Version: cliVersion,
}
env.Init(false) env.Init(false)
defer env.Close() defer env.Close()
switch args[0] { switch args[0] {

View file

@ -40,6 +40,7 @@ Migrates the ~/myconfig.omp.json config file to toml and writes the result to y
A backup of the current config can be found at ~/myconfig.omp.json.bak.`, A backup of the current config can be found at ~/myconfig.omp.json.bak.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
env := &environment.ShellEnvironment{ env := &environment.ShellEnvironment{
Version: cliVersion,
CmdFlags: &environment.Flags{ CmdFlags: &environment.Flags{
Config: config, Config: config,
Migrate: true, Migrate: true,

View file

@ -43,6 +43,7 @@ func init() { // nolint:gochecknoinits
func runInit(shell string) { func runInit(shell string) {
env := &environment.ShellEnvironment{ env := &environment.ShellEnvironment{
Version: cliVersion,
CmdFlags: &environment.Flags{ CmdFlags: &environment.Flags{
Shell: shell, Shell: shell,
Config: config, Config: config,

View file

@ -45,6 +45,7 @@ var printCmd = &cobra.Command{
Args: cobra.OnlyValidArgs, Args: cobra.OnlyValidArgs,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
env := &environment.ShellEnvironment{ env := &environment.ShellEnvironment{
Version: cliVersion,
CmdFlags: &environment.Flags{ CmdFlags: &environment.Flags{
Config: config, Config: config,
PWD: pwd, PWD: pwd,

View file

@ -207,6 +207,7 @@ const (
type ShellEnvironment struct { type ShellEnvironment struct {
CmdFlags *Flags CmdFlags *Flags
Version string
cwd string cwd string
cmdCache *commandCache cmdCache *commandCache
fileCache *fileCache fileCache *fileCache
@ -219,12 +220,9 @@ func (env *ShellEnvironment) Init(debug bool) {
if env.CmdFlags == nil { if env.CmdFlags == nil {
env.CmdFlags = &Flags{} env.CmdFlags = &Flags{}
} }
if len(env.CmdFlags.Config) == 0 {
env.CmdFlags.Config = env.Getenv("POSH_THEME")
}
env.fileCache = &fileCache{} env.fileCache = &fileCache{}
env.fileCache.Init(env.CachePath()) env.fileCache.Init(env.CachePath())
env.ResolveConfigPath() env.resolveConfigPath()
env.cmdCache = &commandCache{ env.cmdCache = &commandCache{
commands: newConcurrentMap(), commands: newConcurrentMap(),
} }
@ -234,27 +232,12 @@ func (env *ShellEnvironment) Init(debug bool) {
} }
} }
func (env *ShellEnvironment) getConfigPath(location string) { func (env *ShellEnvironment) resolveConfigPath() {
cfg, err := env.HTTPRequest(location, 5000) if len(env.CmdFlags.Config) == 0 {
if err != nil { env.CmdFlags.Config = env.Getenv("POSH_THEME")
return
} }
configPath := filepath.Join(env.CachePath(), "config.omp.json") if len(env.CmdFlags.Config) == 0 {
out, err := os.Create(configPath) env.CmdFlags.Config = fmt.Sprintf("https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/v%s/themes/jandedobbeleer.omp.json", env.Version)
if err != nil {
return
}
defer out.Close()
_, err = io.Copy(out, bytes.NewReader(cfg))
if err != nil {
return
}
env.CmdFlags.Config = configPath
}
func (env *ShellEnvironment) ResolveConfigPath() {
if env.CmdFlags == nil || len(env.CmdFlags.Config) == 0 {
return
} }
location, err := url.ParseRequestURI(env.CmdFlags.Config) location, err := url.ParseRequestURI(env.CmdFlags.Config)
if err == nil { if err == nil {
@ -279,6 +262,24 @@ func (env *ShellEnvironment) ResolveConfigPath() {
env.CmdFlags.Config = filepath.Clean(configFile) env.CmdFlags.Config = filepath.Clean(configFile)
} }
func (env *ShellEnvironment) getConfigPath(location string) {
cfg, err := env.HTTPRequest(location, 5000)
if err != nil {
return
}
configPath := filepath.Join(env.CachePath(), "config.omp.json")
out, err := os.Create(configPath)
if err != nil {
return
}
defer out.Close()
_, err = io.Copy(out, bytes.NewReader(cfg))
if err != nil {
return
}
env.CmdFlags.Config = configPath
}
func (env *ShellEnvironment) trace(start time.Time, function string, args ...string) { func (env *ShellEnvironment) trace(start time.Time, function string, args ...string) {
if !env.debug { if !env.debug {
return return