This commit is contained in:
2025-11-01 20:53:38 +01:00
parent aa8851aca0
commit 6e4452ddb9
12 changed files with 457 additions and 106 deletions

View File

@@ -4,15 +4,18 @@ import (
"context"
"flag"
"fmt"
"mirror-sync/cmd/cli/config"
"mirror-sync/pkg/client"
"mirror-sync/pkg/project"
"os"
"path/filepath"
"github.com/google/subcommands"
)
type (
ApplyCmd struct {
projectName string
}
)
@@ -28,10 +31,22 @@ Options:
}
func (p *ApplyCmd) SetFlags(f *flag.FlagSet) {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
f.StringVar(&p.projectName, "project-name", filepath.Base(wd), "set the project name")
}
func (p *ApplyCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
projectConfig, err := project.LoadCurrent()
clientConfig := config.Load()
defaultValues := project.DefaultValues{
DaemonURL: clientConfig.Deamon.URL,
ProjectName: p.projectName,
}
projectConfig, err := project.LoadCurrent(defaultValues)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
return subcommands.ExitFailure

View File

@@ -56,6 +56,6 @@ func print(pr project.Project) {
fmt.Println("------------------")
for _, repo := range pr.Repositories {
fmt.Printf("%-20s | %s -> %s | %s\n", repo.Name, repo.Source, repo.Destination, repo.Schedule)
fmt.Printf("%s | %-20s | %s -> %s | %s\n", repo.UUID, repo.Name, repo.Source, repo.Destination, repo.Schedule)
}
}

View File

@@ -0,0 +1,60 @@
package remove
import (
"context"
"flag"
"fmt"
"mirror-sync/cmd/cli/config"
"mirror-sync/pkg/client"
"mirror-sync/pkg/project"
"os"
"path/filepath"
"github.com/google/subcommands"
)
type (
DownCmd struct {
projectName string
}
)
func (*DownCmd) Name() string { return "down" }
func (*DownCmd) Synopsis() string { return "remove the current project schedule" }
func (*DownCmd) Usage() string {
return `Usage: mirror-sync down
remove the current project
`
}
func (p *DownCmd) SetFlags(f *flag.FlagSet) {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
f.StringVar(&p.projectName, "project-name", filepath.Base(wd), "set the project name")
}
func (p *DownCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
clientConfig := config.Load()
defaultValues := project.DefaultValues{
DaemonURL: clientConfig.Deamon.URL,
ProjectName: p.projectName,
}
projectConfig, err := project.LoadCurrent(defaultValues)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
return subcommands.ExitFailure
}
cli := client.New(projectConfig.ServerURL)
if err := cli.Remove(projectConfig); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}

View File

@@ -0,0 +1,60 @@
package run
import (
"context"
"flag"
"fmt"
"mirror-sync/cmd/cli/config"
"mirror-sync/pkg/client"
"mirror-sync/pkg/project"
"os"
"path/filepath"
"github.com/google/subcommands"
)
type (
RunCmd struct {
}
)
func (*RunCmd) Name() string { return "run" }
func (*RunCmd) Synopsis() string { return "run the current project schedule" }
func (*RunCmd) Usage() string {
return `Usage: mirror-sync run
run the current project
`
}
func (p *RunCmd) SetFlags(f *flag.FlagSet) {
}
func (p *RunCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
clientConfig := config.Load()
wd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
return subcommands.ExitFailure
}
defaultValues := project.DefaultValues{
DaemonURL: clientConfig.Deamon.URL,
ProjectName: filepath.Base(wd),
}
projectConfig, err := project.LoadCurrent(defaultValues)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
return subcommands.ExitFailure
}
cli := client.New(projectConfig.ServerURL)
if err := cli.RunOne(projectConfig); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}

View File

@@ -4,7 +4,10 @@ import (
"context"
"flag"
"fmt"
"mirror-sync/cmd/cli/config"
"mirror-sync/pkg/client"
"mirror-sync/pkg/constants"
"os"
"runtime"
"strconv"
@@ -31,14 +34,31 @@ func (p *VersionCmd) SetFlags(f *flag.FlagSet) {
}
func (p *VersionCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
local()
return subcommands.ExitSuccess
}
func local() {
fmt.Println("Client: mirror-sync cli")
fmt.Println(" Version: " + constants.Version)
fmt.Println(" API version: " + strconv.Itoa(constants.ApiVersion))
fmt.Println(" Go version: " + runtime.Version())
fmt.Println(" OS/Arch: " + runtime.GOOS + "/" + runtime.GOARCH)
clientConfig := config.Load()
cli := client.New(clientConfig.Deamon.URL)
systemInfoDaemon, err := cli.Version()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
return subcommands.ExitFailure
}
fmt.Println()
fmt.Println("Daemon:")
fmt.Println(" Version: " + systemInfoDaemon.Version)
fmt.Println(" API version: " + strconv.Itoa(systemInfoDaemon.APIVersion))
fmt.Println(" Go version: " + systemInfoDaemon.GoVersion)
fmt.Println(" OS/Arch: " + systemInfoDaemon.OSName + "/" + systemInfoDaemon.OSArchitecture)
return subcommands.ExitSuccess
}
func local() {
}

View File

@@ -6,6 +6,8 @@ import (
"fmt"
"mirror-sync/cmd/cli/commands/apply"
"mirror-sync/cmd/cli/commands/list"
"mirror-sync/cmd/cli/commands/remove"
"mirror-sync/cmd/cli/commands/run"
"mirror-sync/cmd/cli/commands/version"
"os"
@@ -25,6 +27,8 @@ func main() {
subcommands.Register(&version.VersionCmd{}, "help")
subcommands.Register(&apply.ApplyCmd{}, "projects")
subcommands.Register(&run.RunCmd{}, "projects")
subcommands.Register(&remove.DownCmd{}, "projects")
subcommands.Register(&list.ListCmd{}, "management")