Skip to content

Commit

Permalink
Allow document update via CLI (#66)
Browse files Browse the repository at this point in the history
Here we simply expose `outline` package's document update facility via
the cli.

This time after gaining more knowledge about the cobra module, the way
I added new command is a bit different. This approach IMO is simpler and
easier with better organization and less verbose.

---------

Co-authored-by: Stefan M. <StefMa@users.noreply.github.com>
  • Loading branch information
rsjethani and StefMa committed Jan 12, 2024
1 parent 4952932 commit cae5458
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"

"github.com/ioki-mobility/go-outline"
"github.com/ioki-mobility/go-outline/internal/common"
Expand Down Expand Up @@ -123,6 +125,7 @@ func parseCmd() *cobra.Command {
rootCmd.AddCommand(documentCmd)
documentCmd.AddCommand(documentCreateCmd)
documentCmd.AddCommand(documentGetCmd)
documentCmd.AddCommand(documentUpdate())

return rootCmd
}
Expand Down Expand Up @@ -233,9 +236,68 @@ func documentGet(serverURL string, apiKey string, id string, idIsShareID bool) e

b, err := json.MarshalIndent(doc, "", " ")
if err != nil {
return fmt.Errorf("failed marshalling document with id '%s: %w", id, err)
return fmt.Errorf("failed marshalling document with id '%s': %w", id, err)
}
fmt.Println(string(b))

return nil
}

func documentUpdate() *cobra.Command {
var title string
var append, publish, readText bool

cmd := &cobra.Command{
Use: "update",
Short: "Update an existing document",
Long: "Update an existing document's title, text etc. properties",
Args: cobra.MinimumNArgs(1),
RunE: func(c *cobra.Command, args []string) error {
id := args[0]
errBase := fmt.Sprintf("failed updating document with ID '%s'", id)

// Extract value of global flags
key, err := c.Flags().GetString(flagApiKey)
if err != nil {
return fmt.Errorf("%s: %w", errBase, err)
}
url, err := c.Flags().GetString(flagServerURL)
if err != nil {
return fmt.Errorf("%s: %w", errBase, err)
}

cl := outline.New(url, &http.Client{}, key).Documents().Update(outline.DocumentID(id)).
Append(append).Publish(publish).Title(title)

if readText {
b, err := io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("%s: %w", errBase, err)
}
cl.Text(string(b))
}

doc, err := cl.Do(context.Background())
if err != nil {
return fmt.Errorf("%s: %w", errBase, err)
}

b, err := json.MarshalIndent(doc, "", " ")
if err != nil {
return fmt.Errorf("failed marshalling document with ID '%s': %w", id, err)
}
fmt.Println(string(b))

return nil
},
}

cmd.Flags().StringVar(&title, "title", "", "The title of the document")
cmd.Flags().BoolVar(&readText, "text", false, "Read document text from stdin")
cmd.Flags().BoolVar(&append, "append", false, "Append new text to existing rather than replacing it")
cmd.Flags().BoolVar(&publish, "publish", false,
"Whether this document should be published and made visible to other team members, if a draft",
)

return cmd
}

0 comments on commit cae5458

Please sign in to comment.