refactor
This commit is contained in:
parent
ca0ebdb999
commit
46dbf59974
@ -1,21 +1,21 @@
|
||||
package commands
|
||||
package boom
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Boom() *cli.Command {
|
||||
func Command() *cli.Command {
|
||||
|
||||
return &cli.Command{
|
||||
Name: "boom",
|
||||
Usage: "explode",
|
||||
Aliases: []string{"b"},
|
||||
Action: boom,
|
||||
Action: action,
|
||||
}
|
||||
}
|
||||
|
||||
func boom(c *cli.Context) error {
|
||||
func action(c *cli.Context) error {
|
||||
fmt.Println("BOOM")
|
||||
return nil
|
||||
}
|
||||
32
commands/bubbleTeaTest/bubbleteaTest.go
Normal file
32
commands/bubbleTeaTest/bubbleteaTest.go
Normal file
@ -0,0 +1,32 @@
|
||||
package bubbleTeaTest
|
||||
|
||||
import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Command() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "BubbleTeaTest",
|
||||
Usage: "USAGE OF COMMAND",
|
||||
Action: action,
|
||||
}
|
||||
}
|
||||
|
||||
func action(*cli.Context) error {
|
||||
p := tea.NewProgram(initialModel())
|
||||
p.Run()
|
||||
return nil
|
||||
}
|
||||
|
||||
func initialModel() model {
|
||||
return model{
|
||||
// Our to-do list is a grocery list
|
||||
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
|
||||
|
||||
// A map which indicates which choices are selected. We're using
|
||||
// the map like a mathematical set. The keys refer to the indexes
|
||||
// of the `choices` slice, above.
|
||||
selected: make(map[int]struct{}),
|
||||
}
|
||||
}
|
||||
@ -1,9 +1,8 @@
|
||||
package commands
|
||||
package bubbleTeaTest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
type model struct {
|
||||
@ -12,34 +11,6 @@ type model struct {
|
||||
selected map[int]struct{} // which to-do items are selected
|
||||
}
|
||||
|
||||
func BubbleTeaTest() *cli.Command {
|
||||
|
||||
return &cli.Command{
|
||||
Name: "BubbleTeaTest",
|
||||
Usage: "USAGE OF COMMAND",
|
||||
Action: bubbleTeaTest,
|
||||
}
|
||||
}
|
||||
|
||||
func bubbleTeaTest(*cli.Context) error {
|
||||
p := tea.NewProgram(initialModel())
|
||||
p.Run()
|
||||
println()
|
||||
return nil
|
||||
}
|
||||
|
||||
func initialModel() model {
|
||||
return model{
|
||||
// Our to-do list is a grocery list
|
||||
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
|
||||
|
||||
// A map which indicates which choices are selected. We're using
|
||||
// the map like a mathematical set. The keys refer to the indexes
|
||||
// of the `choices` slice, above.
|
||||
selected: make(map[int]struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (m model) Init() tea.Cmd {
|
||||
// Just return `nil`, which means "no I/O right now, please."
|
||||
return nil
|
||||
25
commands/config/config.go
Normal file
25
commands/config/config.go
Normal file
@ -0,0 +1,25 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Command() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "config",
|
||||
Usage: "manage config file",
|
||||
Aliases: []string{"cf"},
|
||||
Action: action,
|
||||
Category: "\nManagement",
|
||||
Subcommands: subcommands(),
|
||||
HideHelpCommand: true,
|
||||
}
|
||||
}
|
||||
|
||||
func action(c *cli.Context) error {
|
||||
err := cli.ShowSubcommandHelp(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
52
commands/config/subcommands.go
Normal file
52
commands/config/subcommands.go
Normal file
@ -0,0 +1,52 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/go-github/github"
|
||||
"github.com/urfave/cli/v2"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func subcommands() []*cli.Command {
|
||||
return []*cli.Command{
|
||||
{
|
||||
Name: "create",
|
||||
Usage: "Generates a new configuration file",
|
||||
Action: creatAction,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func creatAction(c *cli.Context) error {
|
||||
var path string
|
||||
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
path = filepath.Dir(homeDir + "\\AppData\\Local\\kleinCommand\\")
|
||||
case "linux":
|
||||
path = filepath.Dir(homeDir + "/.config/kleinCommand")
|
||||
default:
|
||||
return errors.New("unsupported platform")
|
||||
}
|
||||
|
||||
fmt.Println("Creating configuration file")
|
||||
if err := os.MkdirAll(path, 0770); err != nil {
|
||||
return err
|
||||
}
|
||||
client := github.NewClient(nil)
|
||||
_, _, _, _ = client.Repositories.GetContents(context.Background(), "DariusKlein", "kleinCommand", "config.toml", nil)
|
||||
configPath := filepath.Join(path, "/config.toml")
|
||||
|
||||
err := os.WriteFile(configPath, []byte(""), 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Created: " + configPath)
|
||||
return nil
|
||||
}
|
||||
@ -1,46 +0,0 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Template() *cli.Command {
|
||||
|
||||
return &cli.Command{
|
||||
Name: "NAME",
|
||||
Usage: "USAGE OF COMMAND",
|
||||
Aliases: []string{"T"},
|
||||
Action: action,
|
||||
Flags: flags(),
|
||||
Category: "CATEGORY",
|
||||
Subcommands: subcommands(),
|
||||
}
|
||||
}
|
||||
|
||||
func action(c *cli.Context) error {
|
||||
fmt.Println("TEMPLATE RESPONSE")
|
||||
return nil
|
||||
}
|
||||
|
||||
func flags() []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "test",
|
||||
Aliases: []string{"t"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func subcommands() []*cli.Command {
|
||||
return []*cli.Command{
|
||||
{
|
||||
Name: "NAME",
|
||||
Usage: "USAGE OF COMMAND",
|
||||
Aliases: []string{"T"},
|
||||
Action: action,
|
||||
Flags: flags(),
|
||||
Category: "CATEGORY",
|
||||
},
|
||||
}
|
||||
}
|
||||
46
commands/template/template.go
Normal file
46
commands/template/template.go
Normal file
@ -0,0 +1,46 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Command() *cli.Command {
|
||||
|
||||
return &cli.Command{
|
||||
Name: "NAME",
|
||||
Usage: "USAGE OF COMMAND",
|
||||
Aliases: []string{"T"},
|
||||
Action: action,
|
||||
Flags: flags(),
|
||||
Category: "\nTEMPLATE",
|
||||
Subcommands: subcommands(),
|
||||
HideHelpCommand: true,
|
||||
}
|
||||
}
|
||||
|
||||
func action(c *cli.Context) error {
|
||||
fmt.Println("TEMPLATE RESPONSE")
|
||||
return nil
|
||||
}
|
||||
|
||||
func flags() []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "test",
|
||||
Aliases: []string{"t"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func subcommands() []*cli.Command {
|
||||
return []*cli.Command{
|
||||
{
|
||||
Name: "NAME",
|
||||
Usage: "USAGE OF COMMAND",
|
||||
Aliases: []string{"T"},
|
||||
Action: action,
|
||||
Flags: []cli.Flag{},
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -1,20 +1,20 @@
|
||||
package commands
|
||||
package welcome
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Welcome() *cli.Command {
|
||||
func Command() *cli.Command {
|
||||
|
||||
return &cli.Command{
|
||||
Name: "welcome",
|
||||
Usage: "Explains cli tool",
|
||||
Action: welcome,
|
||||
Action: action,
|
||||
}
|
||||
}
|
||||
|
||||
func welcome(*cli.Context) error {
|
||||
func action(*cli.Context) error {
|
||||
fmt.Println("Welcome, \n you can use -h, --help for help.")
|
||||
return nil
|
||||
}
|
||||
0
config.toml
Normal file
0
config.toml
Normal file
2
go.mod
2
go.mod
@ -4,6 +4,7 @@ go 1.22
|
||||
|
||||
require (
|
||||
github.com/charmbracelet/bubbletea v0.26.3
|
||||
github.com/google/go-github v17.0.0+incompatible
|
||||
github.com/urfave/cli/v2 v2.27.2
|
||||
)
|
||||
|
||||
@ -14,6 +15,7 @@ require (
|
||||
github.com/charmbracelet/x/windows v0.1.0 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
|
||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
|
||||
github.com/google/go-querystring v1.1.0 // indirect
|
||||
github.com/mattn/go-localereader v0.0.1 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
|
||||
|
||||
7
go.sum
7
go.sum
@ -12,6 +12,12 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lV
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
|
||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
|
||||
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
|
||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
|
||||
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
|
||||
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
|
||||
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
|
||||
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
|
||||
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
|
||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
@ -40,3 +46,4 @@ golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
|
||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
||||
24
main.go
24
main.go
@ -1,7 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/DariusKlein/kleinCommand/commands"
|
||||
"github.com/DariusKlein/kleinCommand/commands/boom"
|
||||
"github.com/DariusKlein/kleinCommand/commands/bubbleTeaTest"
|
||||
"github.com/DariusKlein/kleinCommand/commands/config"
|
||||
"github.com/DariusKlein/kleinCommand/commands/template"
|
||||
"github.com/DariusKlein/kleinCommand/commands/welcome"
|
||||
"github.com/urfave/cli/v2"
|
||||
"log"
|
||||
"os"
|
||||
@ -9,10 +13,11 @@ import (
|
||||
|
||||
func main() {
|
||||
app := &cli.App{
|
||||
Name: "KleinCommand",
|
||||
Usage: "manage your home server",
|
||||
UsageText: "kleinCommand [category] [command] [arguments...]",
|
||||
Version: "v0.0.1",
|
||||
Name: "KleinCommand",
|
||||
Usage: "manage your home server",
|
||||
UsageText: "kleinCommand [category] [command] [arguments...]",
|
||||
Version: "v0.0.1",
|
||||
HideVersion: true,
|
||||
Authors: []*cli.Author{
|
||||
{
|
||||
Name: "Darius",
|
||||
@ -21,10 +26,11 @@ func main() {
|
||||
},
|
||||
DefaultCommand: "help",
|
||||
Commands: []*cli.Command{
|
||||
commands.Template(),
|
||||
commands.Welcome(),
|
||||
commands.Boom(),
|
||||
commands.BubbleTeaTest(),
|
||||
template.Command(),
|
||||
welcome.Command(),
|
||||
boom.Command(),
|
||||
bubbleTeaTest.Command(),
|
||||
config.Command(),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user