-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new command that crete post from template
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
"github.com/srvc/fail/v4" | ||
) | ||
|
||
func NewNewSubCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "new", | ||
Short: "create new post from template", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// Validation | ||
if len(args) > 1 { | ||
return fail.New("Invalid arguments") | ||
} | ||
|
||
fs := afero.NewOsFs() | ||
cfg, err := LoadConfig(fs) | ||
if err != nil { | ||
return fail.Wrap(err) | ||
} | ||
client, err := NewClient(cfg.AccessToken, cfg.TeamName) | ||
if err != nil { | ||
return fail.Wrap(err) | ||
} | ||
ctx := cmd.Context() | ||
|
||
template, err := cmd.Flags().GetString("template") | ||
if err != nil { | ||
return fail.Wrap(err) | ||
} | ||
|
||
if template != "" { | ||
templatePostId, err := ParsePostIdFromArg(template) | ||
if err != nil { | ||
return fail.Wrap(err) | ||
} | ||
|
||
post, err := client.CreatePostFromTemplate(ctx, templatePostId) | ||
if err != nil { | ||
return fail.Wrap(err) | ||
} | ||
|
||
fmt.Printf("Created: %s\n", post.FullName) | ||
fmt.Println(post.Url) | ||
} else { | ||
panic("Unimplemented") | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().StringP("template", "t", "", "the id of template post") | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters