Files
mirror-sync/cmd/cli/commands/apply/apply.go
2025-11-01 20:53:38 +01:00

63 lines
1.3 KiB
Go

package apply
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
}
)
func (*ApplyCmd) Name() string { return "apply" }
func (*ApplyCmd) Synopsis() string { return "apply the current project settings" }
func (*ApplyCmd) Usage() string {
return `Usage: mirror-sync apply
apply the current project settings
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 {
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.Apply(projectConfig); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}