From 6218e63006ea25a354ed5d27c78c1fa7f45d866e Mon Sep 17 00:00:00 2001 From: Ivan Velichko Date: Sat, 23 Mar 2024 20:32:48 +0000 Subject: [PATCH] inner author loop WIP --- cmd/content/push.go | 105 +++++++++++++++++++++++----------------- internal/api/content.go | 21 +++++--- 2 files changed, 74 insertions(+), 52 deletions(-) diff --git a/cmd/content/push.go b/cmd/content/push.go index 4d01adb..1653018 100644 --- a/cmd/content/push.go +++ b/cmd/content/push.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "slices" "strings" "github.com/spf13/cobra" @@ -70,62 +71,78 @@ func runPushContent(ctx context.Context, cli labcli.CLI, opts *pushOptions) erro if err != nil { return fmt.Errorf("couldn't list remote content files: %w", err) } - fmt.Println(remoteFiles) localFiles, err := listContentFilesLocal(dir) if err != nil { return fmt.Errorf("couldn't list local content files: %w", err) } - for _, file := range localFiles { - cli.PrintAux("Uploading %s\n", file) + var merr error + + // Upload new and update existing files. + for _, abspath := range localFiles { + relpath := strings.TrimPrefix(strings.TrimPrefix(abspath, dir), string(filepath.Separator)) + + cli.PrintAux("Pushing %s\n", relpath) // TODO: If file exists remotely, ask permission to overwrite + if slices.Contains(remoteFiles, relpath) && !opts.force { + if !cli.Confirm(fmt.Sprintf("File %s already exists remotely. Overwrite?", relpath), "Yes", "No") { + cli.PrintAux("Skipping...\n") + continue + } + } + + cli.PrintAux("Uploading...\n") + + if filepath.Ext(relpath) == ".md" { + content, err := os.ReadFile(abspath) + if err != nil { + merr = errors.Join(merr, fmt.Errorf("couldn't read content markdown %s: %w", relpath, err)) + continue + } + + if err := cli.Client().PutContentMarkdown( + ctx, + opts.kind, + opts.name, + relpath, + string(content), + ); err != nil { + merr = errors.Join(merr, fmt.Errorf("couldn't upload content markdown %s: %w", relpath, err)) + } + } else { + if err := cli.Client().UploadContentFile( + ctx, + opts.kind, + opts.name, + relpath, + abspath, + ); err != nil { + merr = errors.Join(merr, fmt.Errorf("couldn't upload content file %s: %w", relpath, err)) + } + } + } + + // Delete remote files that don't exist locally. + for _, relpath := range remoteFiles { + if slices.Contains(localFiles, filepath.Join(dir, relpath)) { + continue + } + + if !opts.force && !cli.Confirm(fmt.Sprintf("File %s doesn't exist locally. Delete remotely?", relpath), "Yes", "No") { + cli.PrintAux("Skipping...\n") + continue + } + + cli.PrintAux("Deleting remote %s\n", relpath) - if err := cli.Client().UploadContentFile( - ctx, - opts.kind, - opts.name, - strings.TrimPrefix(strings.TrimPrefix(file, dir), string(filepath.Separator)), - file, - ); err != nil { - return fmt.Errorf("couldn't upload content file %s: %w", file, err) + if err := cli.Client().DeleteContentFile(ctx, opts.kind, opts.name, relpath); err != nil { + merr = errors.Join(merr, fmt.Errorf("couldn't delete remote content file %s: %w", relpath, err)) } } - // TODO: ... - // for _, file := range remoteFiles { - // if !contains(localFiles, file) { - // cli.PrintAux("Deleting %s\n", file) - - // if err := cli.Client().DeleteContentFile(ctx, opts.kind, opts.name, file); err != nil { - // return fmt.Errorf("couldn't delete content file %s: %w", file, err) - // } - // } - // } - - // TODO: --stream - // cli.PrintAux("Starting content sync in %s...\n", opts.dir) - // - // for ctx.Err() == nil { - // data, err := os.ReadFile(filepath.Join(opts.dir, "index.md")) - // if err != nil { - // return fmt.Errorf("couldn't read index.md: %w", err) - // } - - // if err := cli.Client().PutContentMarkdown(ctx, api.PutContentMarkdownRequest{ - // Kind: opts.kind.String(), - // Name: opts.name, - // Content: string(data), - // }); err != nil { - // return fmt.Errorf("couldn't update content: %w", err) - // } - - // cli.PrintAux("Synced content...\n") - // time.Sleep(5 * time.Second) - // } - - return nil + return merr } func listContentFilesLocal(dir string) ([]string, error) { diff --git a/internal/api/content.go b/internal/api/content.go index 28fa1f4..61bd697 100644 --- a/internal/api/content.go +++ b/internal/api/content.go @@ -10,12 +10,6 @@ import ( "github.com/iximiuz/labctl/internal/content" ) -type PutContentMarkdownRequest struct { - Kind string `json:"kind"` - Name string `json:"name"` - Content string `json:"content"` -} - func (c *Client) ListContentFiles( ctx context.Context, kind content.ContentKind, @@ -32,8 +26,19 @@ func (c *Client) ListContentFiles( return files, nil } -func (c *Client) PutContentMarkdown(ctx context.Context, req PutContentMarkdownRequest) error { - body, err := toJSONBody(req) +func (c *Client) PutContentMarkdown( + ctx context.Context, + kind content.ContentKind, + name string, + file string, + content string, +) error { + body, err := toJSONBody(map[string]string{ + "kind": kind.String(), + "name": name, + "file": file, + "content": content, + }) if err != nil { return err }