/
availables.go
176 lines (146 loc) · 4.45 KB
/
availables.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
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
/*
Copyright © 2021-2023 Macaroni OS Linux
See AUTHORS and LICENSE for the license details and contributors.
*/
package cmdbrowser
import (
"fmt"
"os"
"path/filepath"
"github.com/macaroni-os/macaronictl/pkg/browser"
specs "github.com/macaroni-os/macaronictl/pkg/specs"
"github.com/logrusorgru/aurora"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
func NewAvailableCommand(config *specs.MacaroniCtlConfig) *cobra.Command {
c := &cobra.Command{
Use: "available",
Aliases: []string{"availables", "a"},
Short: "List available browsers and their customization.",
Long: `Shows browsers available in configured repositories.
$ macaronictl browser availables
NOTE: It works only if the repositories are synced.
`,
PreRun: func(cmd *cobra.Command, args []string) {
systemDir, _ := cmd.Flags().GetString("system-dir")
homeDir, _ := cmd.Flags().GetString("home-dir")
if systemDir == "" {
fmt.Println("Invalid system-dir option.")
os.Exit(1)
}
if homeDir == "" {
fmt.Println("Invalid home-dir option.")
os.Exit(1)
}
},
Run: func(cmd *cobra.Command, args []string) {
//log := logger.GetDefaultLogger()
jsonOutput, _ := cmd.Flags().GetBool("json")
catalogFile, _ := cmd.Flags().GetString("catalog-file")
systemDir, _ := cmd.Flags().GetString("system-dir")
homeDir, _ := cmd.Flags().GetString("home-dir")
stones, err := browser.AvailableBrowsers(false, config)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
stonesInstalled, err := browser.AvailableBrowsers(true, config)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
opts := &browser.BrowserConfiguratorOpts{
Systemdir: systemDir,
Homedir: homeDir,
Catalogfile: catalogFile,
}
// Create configurator
configurator, err := browser.NewBrowserConfigurator(opts)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if !jsonOutput {
table := tablewriter.NewWriter(os.Stdout)
table.SetBorders(tablewriter.Border{
Left: true, Top: false, Right: true, Bottom: false,
})
table.SetCenterSeparator("|")
table.SetHeader([]string{
"Package",
"Package Version",
"System Options",
"User Options",
"Engine",
"Binaries",
})
installedStonesMap := stonesInstalled.ToMap()
// Create response struct
for _, s := range stones.Stones {
withSystemOpts := false
withHomeOpts := false
binaries := "N/A"
engine, pkg := configurator.GetSystemConfig().GetEngineAndPackage(s.GetName())
if !configurator.GetSystemConfig().IsEmpty() && pkg != nil &&
pkg.HasOptions() {
withSystemOpts = true
binaries = pkg.Binary
}
homePkg := configurator.GetHomeConfig().GetPackage(s.GetName())
if !configurator.GetHomeConfig().IsEmpty() && homePkg != nil {
withHomeOpts = true
binaries = homePkg.Binary
}
engineName := "N/A"
if engine != nil {
engineName = engine.Name
} else {
// POST: Get engine from catalog.
engine, pkg = configurator.GetCatalog().GetEngineAndPackage(s.GetName())
if engine != nil {
engineName = engine.Name
}
if pkg != nil {
binaries = pkg.Binary
}
}
name := s.GetName()
version := "v" + s.GetVersion()
if _, ok := installedStonesMap.Stones[s.GetName()]; ok {
name = fmt.Sprintf("%s", aurora.Bold(name))
version = fmt.Sprintf("%s", aurora.Bold(version))
}
row := []string{
name,
version,
fmt.Sprintf("%v", withSystemOpts),
fmt.Sprintf("%v", withHomeOpts),
engineName,
binaries,
}
table.Append(row)
}
table.Render()
}
},
}
// Ignoring errors to avoid exceptions on
// containers running macaronictl env-update.
// Without homedir the command will be interrupted
// on PreRun.
homeDir, _ := os.UserHomeDir()
browserConfigsHomedir := ""
if homeDir != "" {
browserConfigsHomedir = filepath.Join(homeDir, ".local/share/macaroni/browsers")
}
flags := c.Flags()
flags.Bool("json", false, "JSON output")
flags.String("catalog-file", "/usr/share/macaroni/browsers/catalog",
"Specify the directory of the catalog file of all engines options.")
flags.String("system-dir", "/etc/macaroni/browsers",
"Override the directory of the system configuration with engines options.")
flags.String("home-dir", browserConfigsHomedir,
"Override the directory of the user with engines options.")
return c
}