forked from noahgorstein/jqp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.go
More file actions
176 lines (144 loc) · 3.89 KB
/
Copy pathroot.go
File metadata and controls
176 lines (144 loc) · 3.89 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package cmd
import (
"errors"
"fmt"
"os"
"github.com/alecthomas/chroma/v2"
"github.com/charmbracelet/bubbletea"
"github.com/noahgorstein/jqp/tui/bubbles/jqplayground"
"github.com/noahgorstein/jqp/tui/theme"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var rootCmd = &cobra.Command{
Version: "0.5.0",
Use: "jqp",
Short: "jqp is a TUI to explore jq",
Long: `jqp is a TUI to explore the jq command line utility`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
configTheme := viper.GetString(configKeysName.themeName)
if !cmd.Flags().Changed(flagsName.theme) {
flags.theme = configTheme
}
themeOverrides := viper.GetStringMapString(configKeysName.themeOverrides)
styleOverrides := viper.GetStringMapString(configKeysName.styleOverrides)
jqtheme, defaultTheme := theme.GetTheme(flags.theme, styleOverrides)
// If not using the default theme,
// and if theme specified is the same as in the config,
// which happens if the theme flag was used,
// apply chroma style overrides.
if !defaultTheme && configTheme == flags.theme && len(themeOverrides) > 0 {
// Reverse chroma.StandardTypes to be keyed by short string
chromaTypes := make(map[string]chroma.TokenType)
for tokenType, short := range chroma.StandardTypes {
chromaTypes[short] = tokenType
}
builder := jqtheme.ChromaStyle.Builder()
for k, v := range themeOverrides {
builder.Add(chromaTypes[k], v)
}
style, err := builder.Build()
if err == nil {
jqtheme.ChromaStyle = style
}
}
if isInputFromPipe() {
stdin := streamToBytes(os.Stdin)
isValidJson := isValidJson(stdin)
if !isValidJson {
return errors.New("JSON is not valid")
}
bubble := jqplayground.New(stdin, "STDIN", jqtheme)
p := tea.NewProgram(bubble, tea.WithAltScreen())
if err := p.Start(); err != nil {
return err
}
return nil
} else {
// get the file
file, e := getFile()
if e != nil {
return e
}
defer file.Close()
// read the file
data, err := os.ReadFile(flags.filepath)
if err != nil {
return err
}
isValidJson := isValidJson(data)
if !isValidJson {
return errors.New("JSON is not valid")
}
// get file info so we can get the filename
fi, err := os.Stat(flags.filepath)
if err != nil {
return err
}
bubble := jqplayground.New(data, fi.Name(), jqtheme)
p := tea.NewProgram(bubble, tea.WithAltScreen())
if err := p.Start(); err != nil {
return err
}
return nil
}
},
}
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
// Search config in home directory
viper.AddConfigPath(home)
// register the config file
viper.SetConfigName(".jqp")
//only read from yaml files
viper.SetConfigType("yaml")
}
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Config file:", viper.ConfigFileUsed(), "was used.")
}
}
var flags struct {
filepath, theme string
}
var flagsName = struct {
file, fileShort, theme, themeShort string
}{
file: "file",
fileShort: "f",
theme: "theme",
themeShort: "t",
}
var configKeysName = struct {
themeName string
themeOverrides string
styleOverrides string
}{
themeName: "theme.name",
themeOverrides: "theme.chromaStyleOverrides",
styleOverrides: "theme.styleOverrides",
}
var cfgFile string
func Execute() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "path to config file (default is $HOME/.jqp.yaml)")
rootCmd.Flags().StringVarP(
&flags.filepath,
flagsName.file,
flagsName.fileShort,
"", "path to the input JSON file")
rootCmd.Flags().StringVarP(
&flags.theme,
flagsName.theme,
flagsName.themeShort,
"", "jqp theme")
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}