wip
This commit is contained in:
61
cmd/cli/commands/list/list.go
Normal file
61
cmd/cli/commands/list/list.go
Normal 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
52
cmd/cli/config/config.go
Normal 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",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,9 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"mirror-sync/cmd/cli/commands/apply"
|
"mirror-sync/cmd/cli/commands/apply"
|
||||||
|
"mirror-sync/cmd/cli/commands/list"
|
||||||
"mirror-sync/cmd/cli/commands/version"
|
"mirror-sync/cmd/cli/commands/version"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@@ -11,6 +13,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "fatal:", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
subcommands.Register(subcommands.HelpCommand(), "help")
|
subcommands.Register(subcommands.HelpCommand(), "help")
|
||||||
subcommands.Register(subcommands.FlagsCommand(), "help")
|
subcommands.Register(subcommands.FlagsCommand(), "help")
|
||||||
subcommands.Register(subcommands.CommandsCommand(), "help")
|
subcommands.Register(subcommands.CommandsCommand(), "help")
|
||||||
@@ -18,6 +26,8 @@ func main() {
|
|||||||
|
|
||||||
subcommands.Register(&apply.ApplyCmd{}, "projects")
|
subcommands.Register(&apply.ApplyCmd{}, "projects")
|
||||||
|
|
||||||
|
subcommands.Register(&list.ListCmd{}, "management")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
|||||||
@@ -233,7 +233,7 @@ func (r *Repository) List() ([]project.Project, error) {
|
|||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
stmt, err := r.db.Prepare("SELECT name, schedule, source, destination WHERE uuid = ?")
|
stmt, err := r.db.Prepare("SELECT name, schedule, source, destination FROM Repositories WHERE uuid = ?")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid syntax: %w", err)
|
return nil, fmt.Errorf("invalid syntax: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,36 @@ func (c *Client) Apply(pr project.Project) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) List() ([]project.Project, error) {
|
||||||
|
url, err := url.JoinPath(c.url, "api", "v1", "projects", "all")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to make url: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to send the request to the server: %s", err)
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
if res.StatusCode != 201 {
|
||||||
|
return nil, fmt.Errorf("failed to send the request to the server: %s: %s", res.Status, toError(res.Body))
|
||||||
|
}
|
||||||
|
|
||||||
|
var prs []project.Project
|
||||||
|
d := json.NewDecoder(res.Body)
|
||||||
|
if err := d.Decode(&prs); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse the server response, is the client you up-to-date? (reason: %s)", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, pr := range prs {
|
||||||
|
pr.ServerURL = c.url
|
||||||
|
prs[i] = pr
|
||||||
|
}
|
||||||
|
|
||||||
|
return prs, nil
|
||||||
|
}
|
||||||
|
|
||||||
func toError(body io.ReadCloser) error {
|
func toError(body io.ReadCloser) error {
|
||||||
var msg SimpleError
|
var msg SimpleError
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user