Skip to content

Commit

Permalink
nicer output after running init
Browse files Browse the repository at this point in the history
  • Loading branch information
NiclasvanEyk committed Jun 7, 2023
1 parent ee2fdf7 commit 3e7598d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
14 changes: 9 additions & 5 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import (

// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
SilenceUsage: true,
Short: "A brief description of your command",
Use: "init",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Expand All @@ -40,9 +39,14 @@ to quickly create a Cobra application.`,
## [Unreleased]
`
fmt.Printf("Initialized empty changelog at %s!\n", changelogPath)

return os.WriteFile(changelogPath, []byte(changelogContents), 0774)
err = os.WriteFile(changelogPath, []byte(changelogContents), 0774)
if err != nil {
return err
}

fmt.Printf("Initialized empty changelog at %s:\n", changelogPath)
return clog.Show(changelogContents)
},
}

Expand Down
8 changes: 6 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "changelog",
Short: "A brief description of your application",
Use: "changelog",
SilenceUsage: true,
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Expand Down Expand Up @@ -37,6 +38,9 @@ func init() {

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.keepac.yaml)")

// Help is already accessible via --help
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})

// Cobra also supports local flags, which will only run
// when this action is called directly.
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
Expand Down
24 changes: 4 additions & 20 deletions cmd/show.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package cmd

import (
"fmt"
"os"

clog "github.com/niclasvaneyk/keepac/internal/changelog"

"github.com/charmbracelet/glamour"
"github.com/spf13/cobra"
)

Expand All @@ -15,32 +13,18 @@ var showCmd = &cobra.Command{
Use: "show",
Short: "Displays the contents of the nearest changelog.",
Long: `Displays the contents of the nearest changelog.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
path, err := clog.ResolvePathToChangelog()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
return err
}

source, err := os.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
return err
}

renderer, _ := glamour.NewTermRenderer(
// detect background color and pick either the default dark or light theme
glamour.WithAutoStyle(),
glamour.WithEnvironmentConfig(),
)

out, err := renderer.Render(string(source))
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}

fmt.Print(out)
return clog.Show(string(source))
},
}

Expand Down
23 changes: 23 additions & 0 deletions internal/changelog/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package changelog

import (
"fmt"

"github.com/charmbracelet/glamour"
)

func Show(contents string) error {
renderer, _ := glamour.NewTermRenderer(
// detect background color and pick either the default dark or light theme
glamour.WithAutoStyle(),
glamour.WithEnvironmentConfig(),
)

out, err := renderer.Render(contents)
if err != nil {
return err
}

fmt.Print(out)
return nil
}

0 comments on commit 3e7598d

Please sign in to comment.