refactoring, adding cli to edit config
This commit is contained in:
85
cmd/cli/commands/add/add.go
Normal file
85
cmd/cli/commands/add/add.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package add
|
||||
|
||||
import (
|
||||
"context"
|
||||
customflag "downloadhub/cmd/cli/flag"
|
||||
"downloadhub/pkg/data"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type (
|
||||
AddCmd struct {
|
||||
slug string
|
||||
description string
|
||||
version string
|
||||
iconURL string
|
||||
out string
|
||||
screenshotURLs customflag.Array
|
||||
}
|
||||
)
|
||||
|
||||
func (*AddCmd) Name() string { return "add" }
|
||||
func (*AddCmd) Synopsis() string { return "add an entry" }
|
||||
func (*AddCmd) Usage() string {
|
||||
return `Usage: ./cli add [OPTIONS] NAME
|
||||
|
||||
Options:
|
||||
`
|
||||
}
|
||||
|
||||
func (p *AddCmd) SetFlags(f *flag.FlagSet) {
|
||||
f.StringVar(&p.slug, "slug", "", "")
|
||||
f.StringVar(&p.description, "description", "", "")
|
||||
f.StringVar(&p.version, "version", "0.0.0", "")
|
||||
f.StringVar(&p.iconURL, "icon", "", "an url or a path to the icon")
|
||||
f.StringVar(&p.out, "out", "./config.json", "path to the configuration file")
|
||||
f.Var(&p.screenshotURLs, "screenshot", "an url or a path to a screenshot file")
|
||||
}
|
||||
|
||||
func (p *AddCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
if len(p.slug) == 0 {
|
||||
p.slug = uuid.NewString()
|
||||
}
|
||||
|
||||
if len(p.screenshotURLs) == 0 {
|
||||
p.screenshotURLs = make(customflag.Array, 0)
|
||||
}
|
||||
|
||||
if f.NArg() == 0 {
|
||||
fmt.Fprintln(os.Stderr, "error: name cannot be empty")
|
||||
return subcommands.ExitUsageError
|
||||
}
|
||||
|
||||
if f.NArg() > 1 {
|
||||
fmt.Fprintln(os.Stderr, "error: this command cannot take more than 1 argument")
|
||||
return subcommands.ExitUsageError
|
||||
}
|
||||
|
||||
d, err := data.Load(p.out)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
d.Softwares = append(d.Softwares, data.Software{
|
||||
Slug: p.slug,
|
||||
Name: f.Arg(0),
|
||||
Description: p.description,
|
||||
Version: p.version,
|
||||
IconURL: p.iconURL,
|
||||
ScreenshotURLs: p.screenshotURLs,
|
||||
DownloadLinks: make([]data.DownloadLink, 0),
|
||||
})
|
||||
|
||||
if err := data.Save(d, p.out); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
Reference in New Issue
Block a user