Skip to content

Commit

Permalink
inner author loop WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
iximiuz committed Mar 23, 2024
1 parent cc2e29b commit 6218e63
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 52 deletions.
105 changes: 61 additions & 44 deletions cmd/content/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 13 additions & 8 deletions internal/api/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand Down

0 comments on commit 6218e63

Please sign in to comment.