Skip to content

Commit

Permalink
feat: add ark update readme command
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rgee0 committed Nov 20, 2024
1 parent b0cf2fb commit f35c6a9
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ mc
/arkade-*
/faas-cli*
test.out
docker-compose.yaml
docker-compose.yaml*
48 changes: 26 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,23 +239,26 @@ arkade system install containerd \
--systemd
```

Run the following to see what's available `arkade system install`:

```
actions-runner Install GitHub Actions Runner
buildkitd Install Buildkitd
caddy Install Caddy Server
cni Install CNI plugins
containerd Install containerd
firecracker Install Firecracker
gitlab-runner Install GitLab Runner
go Install Go
node Install Node.js
prometheus Install Prometheus
pwsh Install Powershell
registry Install registry
tc-redirect-tap Install tc-redirect-tap
```
### Catalog of System Installs:
<!-- start system content -->
| SYSTEM INSTALL | DESCRIPTION |
|-----------------|-------------------------------|
| actions-runner | Install GitHub Actions Runner |
| buildkitd | Install Buildkitd |
| caddy | Install Caddy Server |
| cni | Install CNI plugins |
| containerd | Install containerd |
| firecracker | Install Firecracker |
| gitlab-runner | Install GitLab Runner |
| go | Install Go |
| node | Install Node.js |
| prometheus | Install Prometheus |
| pwsh | Install Powershell |
| registry | Install registry |
| tc-redirect-tap | Install tc-redirect-tap |
There are 13 system installations available.

<!-- end system content -->

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)

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

### Catalog of Apps

| TOOL | DESCRIPTION |
<!-- start apps content -->
| APP | DESCRIPTION |
|-------------------------|---------------------------------------------------------------------|
| argocd | Install argocd |
| cassandra | Install cassandra |
Expand Down Expand Up @@ -749,13 +752,13 @@ A CLI or "tool" is a command line tool that you run directly on your own worksta
| traefik2 | Install traefik2 |
| vault | Install vault |
| waypoint | Install Waypoint |
There are 52 apps that you can install on your cluster.

There are 52 apps that you can install on your cluster.

<!-- end apps content -->
> Note to contributors, run `go build && ./arkade install --print-table` to generate this list

### Catalog of CLIs

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

<!-- end tools content -->
> Note to contributors, run `go build && ./arkade get --format markdown` to generate this list
6 changes: 3 additions & 3 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,17 @@ and provides a fast and easy alternative to a package manager.`,

if len(format) > 0 {
if get.TableFormat(format) == get.MarkdownStyle {
get.CreateToolsTable(tools, get.MarkdownStyle)
fmt.Print(get.CreateToolsTable(tools, get.MarkdownStyle))
} else if get.TableFormat(format) == get.ListStyle {
for _, r := range tools {
fmt.Printf("%s\n", r.Name)
}

} else {
get.CreateToolsTable(tools, get.TableStyle)
fmt.Print(get.CreateToolsTable(tools, get.TableStyle))
}
} else {
get.CreateToolsTable(tools, get.TableStyle)
fmt.Print(get.CreateToolsTable(tools, get.TableStyle))
}
return nil
}
Expand Down
52 changes: 30 additions & 22 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package cmd

import (
"fmt"
"os"
"sort"
"strings"

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

if printTable {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Tool", "Description"})

table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetAutoWrapText(false)

appSortedList := make([]string, 0, len(appList))

for a := range appList {
appSortedList = append(appSortedList, a)
}
sort.Strings(appSortedList)

for _, k := range appSortedList {
table.Append([]string{k, appList[k].Installer().Short})
}

table.Render()

fmt.Printf("\nThere are %d apps that you can install on your cluster.\n", len(appList))
fmt.Print(CreateAppsTable(appList))
return nil
}

Expand Down Expand Up @@ -181,3 +161,31 @@ func NewArkadeApp(cmd func() *cobra.Command, msg string) ArkadeApp {
InfoMessage: msg,
}
}

func CreateAppsTable(apps map[string]ArkadeApp) string {

tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader([]string{"App", "Description"})
table.SetCaption(true,
fmt.Sprintf("\nThere are %d apps that you can install on your cluster.\n", len(apps)))

table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetAutoWrapText(false)

appSortedList := make([]string, 0, len(apps))

for a := range apps {
appSortedList = append(appSortedList, a)
}
sort.Strings(appSortedList)

for _, k := range appSortedList {
table.Append([]string{k, apps[k].Installer().Short})
}

table.Render()

return tableString.String()
}
39 changes: 38 additions & 1 deletion cmd/system/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@

package system

import "github.com/spf13/cobra"
import (
"fmt"
"sort"
"strings"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)

func MakeInstall() *cobra.Command {

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

return command
}

func CreateSystemTable(sys []*cobra.Command) string {

tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader([]string{"System Install", "Description"})
table.SetCaption(true,
fmt.Sprintf("\nThere are %d system installations available.\n", len(sys)))

table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetAutoWrapText(false)

var sysMap = make(map[string]string, len(sys))
sortedList := make([]string, 0, len(sys))

for _, s := range sys {
sysMap[s.Use] = s.Short
sortedList = append(sortedList, s.Use)
}
sort.Strings(sortedList)

for _, sysInst := range sortedList {
table.Append([]string{sysInst, sysMap[sysInst]})
}

table.Render()

return tableString.String()
}
86 changes: 86 additions & 0 deletions cmd/update/readme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) arkade author(s) 2022. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package update

import (
"fmt"
"os"
"sort"
"strings"

install "github.com/alexellis/arkade/cmd"
system "github.com/alexellis/arkade/cmd/system"
"github.com/alexellis/arkade/pkg/get"
"github.com/spf13/cobra"
)

func MakeReadme() *cobra.Command {
var command = &cobra.Command{
Use: "readme",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
}
command.RunE = func(cmd *cobra.Command, args []string) error {

var readmeTables = make(map[string]string)

//update system installs <!-- start system content -->
systemList := system.MakeInstall().Commands()
readmeTables["system"] = system.CreateSystemTable(systemList)

//update apps <!-- start apps content -->
appList := install.GetApps()
readmeTables["apps"] = install.CreateAppsTable(appList)

//update tools <!-- start tools content -->
tools := get.MakeTools()
sort.Sort(tools)
readmeTables["tools"] = get.CreateToolsTable(tools, get.MarkdownStyle)

writeTableToReadme(readmeTables)

return nil
}
return command
}

func writeTableToReadme(tables map[string]string) {

filePath := "README.md"
fileContent, err := os.ReadFile(filePath)
if err != nil {
panic(fmt.Errorf("failed to read file: %w", err))
}
content := string(fileContent)

for k, v := range tables {

startMarker := fmt.Sprintf("<!-- start %s content -->", k)
endMarker := fmt.Sprintf("<!-- end %s content -->", k)

startIdx := strings.Index(content, startMarker)
endIdx := strings.Index(content, endMarker)
if startIdx == -1 || endIdx == -1 || startIdx > endIdx {
fmt.Errorf("%s readme markers not found or are in incorrect order", k)
}

content = content[:startIdx+len(startMarker)] + "\n" +
v + "\n" +
content[endIdx:]

fmt.Printf("updated %s...\n", k)
}

err = os.WriteFile(filePath, []byte(content), 0644)
if err != nil {
panic(fmt.Errorf("failed to write to file: %w", err))
}

fmt.Println("README updated successfully")
}
5 changes: 4 additions & 1 deletion cmd/update.go → cmd/update/update.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) arkade author(s) 2022. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package cmd
package update

import (
"fmt"
Expand Down Expand Up @@ -62,7 +62,10 @@ version twice.`,
fmt.Println("\n", aec.Bold.Apply(pkg.SupportMessageShort))

return nil

}

command.AddCommand(MakeReadme())
return command
}

Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/alexellis/arkade/cmd/chart"
"github.com/alexellis/arkade/cmd/oci"
"github.com/alexellis/arkade/cmd/system"
"github.com/alexellis/arkade/cmd/update"
"github.com/spf13/cobra"
)

Expand All @@ -27,7 +28,7 @@ func main() {
rootCmd.AddCommand(cmd.MakeInstall())
rootCmd.AddCommand(cmd.MakeVersion())
rootCmd.AddCommand(cmd.MakeInfo())
rootCmd.AddCommand(cmd.MakeUpdate())
rootCmd.AddCommand(update.MakeUpdate())
rootCmd.AddCommand(cmd.MakeGet())
rootCmd.AddCommand(cmd.MakeUninstall())
rootCmd.AddCommand(cmd.MakeShellCompletion())
Expand Down
11 changes: 8 additions & 3 deletions pkg/get/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package get

import (
"fmt"
"os"
"strings"

"github.com/olekukonko/tablewriter"
)
Expand All @@ -16,8 +16,11 @@ const (
)

// CreateToolTable creates table to show the avaiable CLI tools
func CreateToolsTable(tools Tools, format TableFormat) {
table := tablewriter.NewWriter(os.Stdout)
func CreateToolsTable(tools Tools, format TableFormat) string {

tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
//table := tablewriter.NewWriter(os.Stdout)

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

table.Render()

return tableString.String()
}

0 comments on commit f35c6a9

Please sign in to comment.