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.`,
Run: func(cmd *cobra.Command, args []string) {
env := &environment.ShellEnvironment{
Version: cliVersion,
CmdFlags: &environment.Flags{
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`,
Run: func(cmd *cobra.Command, args []string) {
env := &environment.ShellEnvironment{
Version: cliVersion,
CmdFlags: &environment.Flags{
Config: config,
Shell: "shell",

View file

@ -29,7 +29,9 @@ This command is used to get the value of the following variables:
},
Args: cobra.OnlyValidArgs,
Run: func(cmd *cobra.Command, args []string) {
env := &environment.ShellEnvironment{}
env := &environment.ShellEnvironment{
Version: cliVersion,
}
env.Init(false)
defer env.Close()
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.`,
Run: func(cmd *cobra.Command, args []string) {
env := &environment.ShellEnvironment{
Version: cliVersion,
CmdFlags: &environment.Flags{
Config: config,
Migrate: true,

View file

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

View file

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

View file

@ -207,6 +207,7 @@ const (
type ShellEnvironment struct {
CmdFlags *Flags
Version string
cwd string
cmdCache *commandCache
fileCache *fileCache
@ -219,12 +220,9 @@ func (env *ShellEnvironment) Init(debug bool) {
if env.CmdFlags == nil {
env.CmdFlags = &Flags{}
}
if len(env.CmdFlags.Config) == 0 {
env.CmdFlags.Config = env.Getenv("POSH_THEME")
}
env.fileCache = &fileCache{}
env.fileCache.Init(env.CachePath())
env.ResolveConfigPath()
env.resolveConfigPath()
env.cmdCache = &commandCache{
commands: newConcurrentMap(),
}
@ -234,27 +232,12 @@ func (env *ShellEnvironment) Init(debug bool) {
}
}
func (env *ShellEnvironment) getConfigPath(location string) {
cfg, err := env.HTTPRequest(location, 5000)
if err != nil {
return
func (env *ShellEnvironment) resolveConfigPath() {
if len(env.CmdFlags.Config) == 0 {
env.CmdFlags.Config = env.Getenv("POSH_THEME")
}
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) ResolveConfigPath() {
if env.CmdFlags == nil || len(env.CmdFlags.Config) == 0 {
return
if len(env.CmdFlags.Config) == 0 {
env.CmdFlags.Config = fmt.Sprintf("https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/v%s/themes/jandedobbeleer.omp.json", env.Version)
}
location, err := url.ParseRequestURI(env.CmdFlags.Config)
if err == nil {
@ -279,6 +262,24 @@ func (env *ShellEnvironment) ResolveConfigPath() {
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) {
if !env.debug {
return