forked from charmbracelet/mods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (115 loc) · 3.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"io"
"os"
"runtime/debug"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glow/editor"
"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-isatty"
"github.com/muesli/termenv"
flag "github.com/spf13/pflag"
)
// Build vars.
var (
//nolint: gochecknoglobals
version = "dev"
commit = ""
date = ""
builtBy = ""
)
func buildVersion() string {
result := "mods version " + version
if commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
}
if date != "" {
result = fmt.Sprintf("%s\nbuilt at: %s", result, date)
}
if builtBy != "" {
result = fmt.Sprintf("%s\nbuilt by: %s", result, builtBy)
}
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
result = fmt.Sprintf("%s\nmodule version: %s, checksum: %s", result, info.Main.Version, info.Main.Sum)
}
return result
}
func exitError(mods *Mods, err error, reason string) {
mods.Error = &modsError{reason: reason, err: err}
fmt.Println(mods.ErrorView())
os.Exit(1)
}
func main() {
renderer := lipgloss.NewRenderer(os.Stderr, termenv.WithColorCache(true))
opts := []tea.ProgramOption{tea.WithOutput(renderer.Output())}
if !isatty.IsTerminal(os.Stdin.Fd()) {
opts = append(opts, tea.WithInput(nil))
}
mods := newMods(renderer)
p := tea.NewProgram(mods, opts...)
m, err := p.Run()
if err != nil {
exitError(mods, err, "Couldn't start Bubble Tea program.")
}
mods = m.(*Mods)
if mods.Error != nil {
os.Exit(1)
}
if mods.Config.Settings {
c := editor.Cmd(mods.Config.SettingsPath)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
exitError(mods, err, "Missing $EDITOR")
}
fmt.Println("Wrote config file to:", mods.Config.SettingsPath)
os.Exit(0)
}
if mods.Config.ResetSettings {
_, err := os.Stat(mods.Config.SettingsPath)
if err != nil {
exitError(mods, err, "Couldn't read config file.")
}
inputFile, err := os.Open(mods.Config.SettingsPath)
if err != nil {
exitError(mods, err, "Couldn't open config file.")
}
defer inputFile.Close() //nolint:errcheck
outputFile, err := os.Create(mods.Config.SettingsPath + ".bak")
if err != nil {
exitError(mods, err, "Couldn't backup config file.")
}
defer outputFile.Close() //nolint:errcheck
_, err = io.Copy(outputFile, inputFile)
if err != nil {
exitError(mods, err, "Couldn't write config file.")
}
// The copy was successful, so now delete the original file
err = os.Remove(mods.Config.SettingsPath)
if err != nil {
exitError(mods, err, "Couldn't remove config file.")
}
err = writeConfigFile(mods.Config.SettingsPath)
if err != nil {
exitError(mods, err, "Couldn't write new config file.")
}
fmt.Println("\n Settings restored to defaults!")
fmt.Printf(
"\n %s %s\n\n",
mods.Styles.Comment.Render("Your old settings are have been saved to:"),
mods.Styles.Link.Render(mods.Config.SettingsPath+".bak"),
)
os.Exit(0)
}
if mods.Config.Version {
fmt.Println(buildVersion())
os.Exit(0)
}
if mods.Config.ShowHelp || (mods.Input == "" && mods.Config.Prefix == "") {
flag.Usage()
os.Exit(0)
}
fmt.Println(mods.FormattedOutput())
}