move code into separate function for legibility

This commit is contained in:
Kovid Goyal
2024-07-26 22:43:13 +05:30
parent bb3aecd2a5
commit a50bf2c6bb

View File

@@ -29,29 +29,7 @@ type Options struct {
InjectSelfOntoPath string
}
func main(args []string, opts *Options) (rc int, err error) {
if len(args) > 0 {
tui.RunCommandRestoringTerminalToSaneStateAfter(args)
}
env_before := os.Environ()
changed := false
for _, entry := range opts.Env {
k, v, found := strings.Cut(entry, "=")
if found {
if err := os.Setenv(k, v); err != nil {
return 1, fmt.Errorf("Failed to set the env var %s with error: %w", k, err)
}
} else {
if err := os.Unsetenv(k); err != nil {
return 1, fmt.Errorf("Failed to unset the env var %s with error: %w", k, err)
}
}
changed = true
}
if os.Getenv("TERM") == "" {
os.Setenv("TERM", kitty.DefaultTermName)
}
if opts.InjectSelfOntoPath == "always" || (opts.InjectSelfOntoPath == "unless-root" && os.Geteuid() != 0) {
func inject_self_onto_path() {
if exe, err := os.Executable(); err == nil {
if exe_dir, err := filepath.Abs(exe); err == nil {
realpath := func(x string) string {
@@ -97,6 +75,32 @@ func main(args []string, opts *Options) (rc int, err error) {
}
}
}
func main(args []string, opts *Options) (rc int, err error) {
if len(args) > 0 {
tui.RunCommandRestoringTerminalToSaneStateAfter(args)
}
env_before := os.Environ()
changed := false
for _, entry := range opts.Env {
k, v, found := strings.Cut(entry, "=")
if found {
if err := os.Setenv(k, v); err != nil {
return 1, fmt.Errorf("Failed to set the env var %s with error: %w", k, err)
}
} else {
if err := os.Unsetenv(k); err != nil {
return 1, fmt.Errorf("Failed to unset the env var %s with error: %w", k, err)
}
}
changed = true
}
if os.Getenv("TERM") == "" {
os.Setenv("TERM", kitty.DefaultTermName)
}
if opts.InjectSelfOntoPath == "always" || (opts.InjectSelfOntoPath == "unless-root" && os.Geteuid() != 0) {
inject_self_onto_path()
}
if term := os.Getenv("TERM"); term == kitty.DefaultTermName && shell_integration.PathToTerminfoDb(term) == "" {
if terminfo_dir, err := shell_integration.EnsureTerminfoFiles(); err == nil {
os.Unsetenv("TERMINFO")