Files
oh-my-posh/src/cli/config_export.go
T
ClaudeandJan De Dobbeleer 2a59ed1e50 refactor(cli): replace cobra and pflag with minimal internal packages
The CLI framework was the largest remaining third-party chunk (545 kB)
and most of its surface went unused - completions are explicitly
disabled, templates and command groups never used. Replace both with
internal packages mirroring the exact API subset in use: a command tree
with persistent flags and nearest-hook semantics, POSIX flag parsing
(--flag=value, --flag value, shorthands and grouping, the -- terminator,
interspersed positionals, unknown-flag allowlisting for the argocd
segment), positional validators, the implicit help command, and the
Windows Explorer double-click guard.

Help, usage and error output were verified byte-identical against the
previous binary across a 47-case golden battery: every command and
subcommand help screen, error formats and exit codes, flag styles
including flags before the subcommand, and init/print rendering.

Shrinks the stripped linux/amd64 binary by 586 kB (13.50 MB -> 12.91 MB;
23.7% below the original 16.93 MB).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qiyvpiy5jR2tyzwZ3zUki5
2026-07-31 07:40:24 +02:00

114 lines
2.5 KiB
Go

package cli
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/config"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/path"
"github.com/jandedobbeleer/oh-my-posh/src/cmdtree"
)
var (
format string
output string
)
var exportCmd = &cmdtree.Command{
Use: "export",
Short: "Export your config",
Long: `Export your config.
You can choose to print the output to stdout, or export your config in the format of your choice.
Example usage:
> oh-my-posh config export --config ~/myconfig.omp.json --format toml
Exports the config file "~/myconfig.omp.json" in TOML format and prints the result to stdout.
> oh-my-posh config export --output ~/new_config.omp.json
Exports the current config to "~/new_config.omp.json" (in JSON format).`,
Args: cmdtree.NoArgs,
Run: func(_ *cmdtree.Command, _ []string) {
if output == "" && format == "" {
// usage error
fmt.Println("neither output path nor export format is specified")
exitcode = 2
return
}
cache.Init(os.Getenv("POSH_SHELL"))
setConfigFlag()
cfg := config.Load(configFlag)
validateExportFormat := func() error {
format = strings.ToLower(format)
switch format {
case config.JSON, config.JSONC:
format = config.JSON
case config.TOML, config.TML:
format = config.TOML
case config.YAML, config.YML:
format = config.YAML
default:
formats := []string{config.JSON, config.JSONC, config.TOML, config.TML, config.YAML, config.YML}
// usage error
fmt.Printf("export format must be one of these: %s\n", strings.Join(formats, ", "))
exitcode = 2
return errors.New("invalid export format")
}
return nil
}
if len(format) != 0 {
if err := validateExportFormat(); err != nil {
return
}
}
if output == "" {
fmt.Print(cfg.Export(format))
return
}
cfg.Source = cleanOutputPath(output)
if format == "" {
format = strings.TrimPrefix(filepath.Ext(output), ".")
if err := validateExportFormat(); err != nil {
return
}
}
cfg.Write(format)
},
}
func cleanOutputPath(output string) string {
output = path.ReplaceTildePrefixWithHomeDir(output)
if !filepath.IsAbs(output) {
if absPath, err := filepath.Abs(output); err == nil {
output = absPath
}
}
return filepath.Clean(output)
}
func init() {
exportCmd.Flags().StringVarP(&format, "format", "f", "json", "config format to migrate to")
exportCmd.Flags().StringVarP(&output, "output", "o", "", "config file to export to")
configCmd.AddCommand(exportCmd)
}