This commit is contained in:
2025-10-25 20:03:36 +02:00
parent 381a4e4988
commit 6224e63fa9
5 changed files with 154 additions and 1 deletions

View File

@@ -0,0 +1,61 @@
package list
import (
"context"
"flag"
"fmt"
"mirror-sync/cmd/cli/config"
"mirror-sync/pkg/client"
"mirror-sync/pkg/project"
"os"
"github.com/google/subcommands"
)
type (
ListCmd struct {
projectName string
}
)
func (*ListCmd) Name() string { return "list" }
func (*ListCmd) Synopsis() string { return "list the scheduled projects" }
func (*ListCmd) Usage() string {
return `Usage: mirror-sync list [--project-name]
list the scheduled projects
Options:
`
}
func (p *ListCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.projectName, "project-name", "", "show only one project")
}
func (p *ListCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
clientConfig := config.Load()
cli := client.New(clientConfig.Deamon.URL)
prs, err := cli.List()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
return subcommands.ExitFailure
}
for _, pr := range prs {
print(pr)
}
return subcommands.ExitSuccess
}
func print(pr project.Project) {
fmt.Println(pr.Name)
fmt.Println("------------------")
for _, repo := range pr.Repositories {
fmt.Printf("%-20s | %s -> %s | %s\n", repo.Name, repo.Source, repo.Destination, repo.Schedule)
}
}

52
cmd/cli/config/config.go Normal file
View File

@@ -0,0 +1,52 @@
package config
import (
"encoding/json"
"errors"
"os"
"path/filepath"
)
type (
ClientConfiguration struct {
Deamon ClientDaemonConfiguration `json:"daemon"`
}
ClientDaemonConfiguration struct {
URL string `json:"url"`
}
)
func Load() ClientConfiguration {
userConfigDir, err := os.UserConfigDir()
if err != nil {
panic("failed to get user config path: " + err.Error())
}
path := filepath.Join(userConfigDir, "mirror-sync", "config.json")
f, err := os.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return Default()
}
panic("failed to load configuration file (" + path + "): " + err.Error())
}
defer f.Close()
var c ClientConfiguration
d := json.NewDecoder(f)
if err := d.Decode(&c); err != nil {
panic("failed to read the configuration file: " + err.Error())
}
return c
}
func Default() ClientConfiguration {
return ClientConfiguration{
Deamon: ClientDaemonConfiguration{
URL: "http://localhost:8080",
},
}
}

View File

@@ -3,7 +3,9 @@ package main
import (
"context"
"flag"
"fmt"
"mirror-sync/cmd/cli/commands/apply"
"mirror-sync/cmd/cli/commands/list"
"mirror-sync/cmd/cli/commands/version"
"os"
@@ -11,6 +13,12 @@ import (
)
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Fprintln(os.Stderr, "fatal:", r)
}
}()
subcommands.Register(subcommands.HelpCommand(), "help")
subcommands.Register(subcommands.FlagsCommand(), "help")
subcommands.Register(subcommands.CommandsCommand(), "help")
@@ -18,6 +26,8 @@ func main() {
subcommands.Register(&apply.ApplyCmd{}, "projects")
subcommands.Register(&list.ListCmd{}, "management")
flag.Parse()
ctx := context.Background()