Skip to content

Commit 0d62388

Browse files
committed
feat: add ark update readme command
Adds a subcommand to update that will update the README tables in situ. This will be useful to contributors who are adding system installs / apps / tools as it will remove the need for the existing manual mechanism to update the appropriate tables in the readme. Running `arkade update readme` will update each of the three tables in the readme, ready to be committed with their change. Signed-off-by: Richard Gee <[email protected]>
1 parent b0cf2fb commit 0d62388

10 files changed

+199
-54
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ mc
88
/arkade-*
99
/faas-cli*
1010
test.out
11-
docker-compose.yaml
11+
docker-compose.yaml*

README.md

+26-22
Original file line numberDiff line numberDiff line change
@@ -239,23 +239,26 @@ arkade system install containerd \
239239
--systemd
240240
```
241241

242-
Run the following to see what's available `arkade system install`:
243-
244-
```
245-
actions-runner Install GitHub Actions Runner
246-
buildkitd Install Buildkitd
247-
caddy Install Caddy Server
248-
cni Install CNI plugins
249-
containerd Install containerd
250-
firecracker Install Firecracker
251-
gitlab-runner Install GitLab Runner
252-
go Install Go
253-
node Install Node.js
254-
prometheus Install Prometheus
255-
pwsh Install Powershell
256-
registry Install registry
257-
tc-redirect-tap Install tc-redirect-tap
258-
```
242+
### Catalog of System Installs:
243+
<!-- start system content -->
244+
| SYSTEM INSTALL | DESCRIPTION |
245+
|-----------------|-------------------------------|
246+
| actions-runner | Install GitHub Actions Runner |
247+
| buildkitd | Install Buildkitd |
248+
| caddy | Install Caddy Server |
249+
| cni | Install CNI plugins |
250+
| containerd | Install containerd |
251+
| firecracker | Install Firecracker |
252+
| gitlab-runner | Install GitLab Runner |
253+
| go | Install Go |
254+
| node | Install Node.js |
255+
| prometheus | Install Prometheus |
256+
| pwsh | Install Powershell |
257+
| registry | Install registry |
258+
| tc-redirect-tap | Install tc-redirect-tap |
259+
There are 13 system installations available.
260+
261+
<!-- end system content -->
259262

260263
The initial set of system apps is now complete, learn more in the original proposal: [Feature: system packages for Linux servers, CI and workstations #654](https://github.com/alexellis/arkade/issues/654)
261264

@@ -694,8 +697,8 @@ An app is software or an add-on for your Kubernetes cluster.
694697
A CLI or "tool" is a command line tool that you run directly on your own workstation or a CI runner.
695698

696699
### Catalog of Apps
697-
698-
| TOOL | DESCRIPTION |
700+
<!-- start apps content -->
701+
| APP | DESCRIPTION |
699702
|-------------------------|---------------------------------------------------------------------|
700703
| argocd | Install argocd |
701704
| cassandra | Install cassandra |
@@ -749,13 +752,13 @@ A CLI or "tool" is a command line tool that you run directly on your own worksta
749752
| traefik2 | Install traefik2 |
750753
| vault | Install vault |
751754
| waypoint | Install Waypoint |
755+
There are 52 apps that you can install on your cluster.
752756

753-
There are 52 apps that you can install on your cluster.
754-
757+
<!-- end apps content -->
755758
> Note to contributors, run `go build && ./arkade install --print-table` to generate this list
756759

757760
### Catalog of CLIs
758-
761+
<!-- start tools content -->
759762
| TOOL | DESCRIPTION |
760763
|------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
761764
| [actions-usage](https://github.com/self-actuated/actions-usage) | Get usage insights from GitHub Actions. |
@@ -922,4 +925,5 @@ There are 52 apps that you can install on your cluster.
922925
| [yt-dlp](https://github.com/yt-dlp/yt-dlp) | Fork of youtube-dl with additional features and fixes |
923926
There are 162 tools, use `arkade get NAME` to download one.
924927

928+
<!-- end tools content -->
925929
> Note to contributors, run `go build && ./arkade get --format markdown` to generate this list

cmd/get.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ and provides a fast and easy alternative to a package manager.`,
7474

7575
if len(format) > 0 {
7676
if get.TableFormat(format) == get.MarkdownStyle {
77-
get.CreateToolsTable(tools, get.MarkdownStyle)
77+
fmt.Print(get.CreateToolsTable(tools, get.MarkdownStyle))
7878
} else if get.TableFormat(format) == get.ListStyle {
7979
for _, r := range tools {
8080
fmt.Printf("%s\n", r.Name)
8181
}
8282

8383
} else {
84-
get.CreateToolsTable(tools, get.TableStyle)
84+
fmt.Print(get.CreateToolsTable(tools, get.TableStyle))
8585
}
8686
} else {
87-
get.CreateToolsTable(tools, get.TableStyle)
87+
fmt.Print(get.CreateToolsTable(tools, get.TableStyle))
8888
}
8989
return nil
9090
}

cmd/install.go

+30-22
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ package cmd
55

66
import (
77
"fmt"
8-
"os"
98
"sort"
9+
"strings"
1010

1111
"github.com/olekukonko/tablewriter"
1212
"github.com/spf13/cobra"
@@ -46,27 +46,7 @@ command.`,
4646
printTable, _ := command.Flags().GetBool("print-table")
4747

4848
if printTable {
49-
table := tablewriter.NewWriter(os.Stdout)
50-
table.SetHeader([]string{"Tool", "Description"})
51-
52-
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
53-
table.SetCenterSeparator("|")
54-
table.SetAutoWrapText(false)
55-
56-
appSortedList := make([]string, 0, len(appList))
57-
58-
for a := range appList {
59-
appSortedList = append(appSortedList, a)
60-
}
61-
sort.Strings(appSortedList)
62-
63-
for _, k := range appSortedList {
64-
table.Append([]string{k, appList[k].Installer().Short})
65-
}
66-
67-
table.Render()
68-
69-
fmt.Printf("\nThere are %d apps that you can install on your cluster.\n", len(appList))
49+
fmt.Print(CreateAppsTable(appList))
7050
return nil
7151
}
7252

@@ -181,3 +161,31 @@ func NewArkadeApp(cmd func() *cobra.Command, msg string) ArkadeApp {
181161
InfoMessage: msg,
182162
}
183163
}
164+
165+
func CreateAppsTable(apps map[string]ArkadeApp) string {
166+
167+
tableString := &strings.Builder{}
168+
table := tablewriter.NewWriter(tableString)
169+
table.SetHeader([]string{"App", "Description"})
170+
table.SetCaption(true,
171+
fmt.Sprintf("\nThere are %d apps that you can install on your cluster.\n", len(apps)))
172+
173+
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
174+
table.SetCenterSeparator("|")
175+
table.SetAutoWrapText(false)
176+
177+
appSortedList := make([]string, 0, len(apps))
178+
179+
for a := range apps {
180+
appSortedList = append(appSortedList, a)
181+
}
182+
sort.Strings(appSortedList)
183+
184+
for _, k := range appSortedList {
185+
table.Append([]string{k, apps[k].Installer().Short})
186+
}
187+
188+
table.Render()
189+
190+
return tableString.String()
191+
}

cmd/system/install.go

+38-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44
package system
55

6-
import "github.com/spf13/cobra"
6+
import (
7+
"fmt"
8+
"sort"
9+
"strings"
10+
11+
"github.com/olekukonko/tablewriter"
12+
"github.com/spf13/cobra"
13+
)
714

815
func MakeInstall() *cobra.Command {
916

@@ -37,3 +44,33 @@ func MakeInstall() *cobra.Command {
3744

3845
return command
3946
}
47+
48+
func CreateSystemTable(sys []*cobra.Command) string {
49+
50+
tableString := &strings.Builder{}
51+
table := tablewriter.NewWriter(tableString)
52+
table.SetHeader([]string{"System Install", "Description"})
53+
table.SetCaption(true,
54+
fmt.Sprintf("\nThere are %d system installations available.\n", len(sys)))
55+
56+
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
57+
table.SetCenterSeparator("|")
58+
table.SetAutoWrapText(false)
59+
60+
var sysMap = make(map[string]string, len(sys))
61+
sortedList := make([]string, 0, len(sys))
62+
63+
for _, s := range sys {
64+
sysMap[s.Use] = s.Short
65+
sortedList = append(sortedList, s.Use)
66+
}
67+
sort.Strings(sortedList)
68+
69+
for _, sysInst := range sortedList {
70+
table.Append([]string{sysInst, sysMap[sysInst]})
71+
}
72+
73+
table.Render()
74+
75+
return tableString.String()
76+
}

cmd/update/readme.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) arkade author(s) 2022. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
package update
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"sort"
10+
"strings"
11+
12+
install "github.com/alexellis/arkade/cmd"
13+
system "github.com/alexellis/arkade/cmd/system"
14+
"github.com/alexellis/arkade/pkg/get"
15+
"github.com/spf13/cobra"
16+
)
17+
18+
func MakeReadme() *cobra.Command {
19+
var command = &cobra.Command{
20+
Use: "readme",
21+
Short: "A brief description of your command",
22+
Long: `A longer description that spans multiple lines and likely contains examples
23+
and usage of using your command. For example:
24+
25+
Cobra is a CLI library for Go that empowers applications.
26+
This application is a tool to generate the needed files
27+
to quickly create a Cobra application.`,
28+
}
29+
command.RunE = func(cmd *cobra.Command, args []string) error {
30+
31+
var readmeTables = make(map[string]string)
32+
33+
//update system installs <!-- start system content -->
34+
systemList := system.MakeInstall().Commands()
35+
readmeTables["system"] = system.CreateSystemTable(systemList)
36+
37+
//update apps <!-- start apps content -->
38+
appList := install.GetApps()
39+
readmeTables["apps"] = install.CreateAppsTable(appList)
40+
41+
//update tools <!-- start tools content -->
42+
tools := get.MakeTools()
43+
sort.Sort(tools)
44+
readmeTables["tools"] = get.CreateToolsTable(tools, get.MarkdownStyle)
45+
46+
writeTableToReadme(readmeTables)
47+
48+
return nil
49+
}
50+
return command
51+
}
52+
53+
func writeTableToReadme(tables map[string]string) {
54+
55+
filePath := "README.md"
56+
fileContent, err := os.ReadFile(filePath)
57+
if err != nil {
58+
panic(fmt.Errorf("failed to read file: %w", err))
59+
}
60+
content := string(fileContent)
61+
62+
for k, v := range tables {
63+
64+
startMarker := fmt.Sprintf("<!-- start %s content -->", k)
65+
endMarker := fmt.Sprintf("<!-- end %s content -->", k)
66+
67+
startIdx := strings.Index(content, startMarker)
68+
endIdx := strings.Index(content, endMarker)
69+
if startIdx == -1 || endIdx == -1 || startIdx > endIdx {
70+
fmt.Errorf("%s readme markers not found or are in incorrect order", k)
71+
}
72+
73+
content = content[:startIdx+len(startMarker)] + "\n" +
74+
v + "\n" +
75+
content[endIdx:]
76+
77+
fmt.Printf("updated %s...\n", k)
78+
}
79+
80+
err = os.WriteFile(filePath, []byte(content), 0644)
81+
if err != nil {
82+
panic(fmt.Errorf("failed to write to file: %w", err))
83+
}
84+
85+
fmt.Println("README updated successfully")
86+
}

cmd/update.go cmd/update/update.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) arkade author(s) 2022. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
package cmd
4+
package update
55

66
import (
77
"fmt"
@@ -62,7 +62,10 @@ version twice.`,
6262
fmt.Println("\n", aec.Bold.Apply(pkg.SupportMessageShort))
6363

6464
return nil
65+
6566
}
67+
68+
command.AddCommand(MakeReadme())
6669
return command
6770
}
6871

docker-compose.yaml.hold

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
postgres:16

main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/alexellis/arkade/cmd/chart"
1111
"github.com/alexellis/arkade/cmd/oci"
1212
"github.com/alexellis/arkade/cmd/system"
13+
"github.com/alexellis/arkade/cmd/update"
1314
"github.com/spf13/cobra"
1415
)
1516

@@ -27,7 +28,7 @@ func main() {
2728
rootCmd.AddCommand(cmd.MakeInstall())
2829
rootCmd.AddCommand(cmd.MakeVersion())
2930
rootCmd.AddCommand(cmd.MakeInfo())
30-
rootCmd.AddCommand(cmd.MakeUpdate())
31+
rootCmd.AddCommand(update.MakeUpdate())
3132
rootCmd.AddCommand(cmd.MakeGet())
3233
rootCmd.AddCommand(cmd.MakeUninstall())
3334
rootCmd.AddCommand(cmd.MakeShellCompletion())

pkg/get/table.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package get
22

33
import (
44
"fmt"
5-
"os"
5+
"strings"
66

77
"github.com/olekukonko/tablewriter"
88
)
@@ -16,8 +16,11 @@ const (
1616
)
1717

1818
// CreateToolTable creates table to show the avaiable CLI tools
19-
func CreateToolsTable(tools Tools, format TableFormat) {
20-
table := tablewriter.NewWriter(os.Stdout)
19+
func CreateToolsTable(tools Tools, format TableFormat) string {
20+
21+
tableString := &strings.Builder{}
22+
table := tablewriter.NewWriter(tableString)
23+
//table := tablewriter.NewWriter(os.Stdout)
2124

2225
table.SetCaption(true,
2326
fmt.Sprintf("There are %d tools, use `arkade get NAME` to download one.", len(tools)))
@@ -50,4 +53,6 @@ func CreateToolsTable(tools Tools, format TableFormat) {
5053
}
5154

5255
table.Render()
56+
57+
return tableString.String()
5358
}

0 commit comments

Comments
 (0)