Skip to content

Commit

Permalink
Display CLI changelog with 'doppler changelog' command
Browse files Browse the repository at this point in the history
This pulls the changelog from cli.doppler.com/changes.
  • Loading branch information
Piccirello committed Oct 17, 2020
1 parent 0040eec commit 7c496ca
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pkg/cmd/changelog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright © 2020 Doppler <support@doppler.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"github.com/DopplerHQ/cli/pkg/http"
"github.com/DopplerHQ/cli/pkg/models"
"github.com/DopplerHQ/cli/pkg/printer"
"github.com/DopplerHQ/cli/pkg/utils"
"github.com/spf13/cobra"
)

var changelogCmd = &cobra.Command{
Use: "changelog",
Short: "view the CLI's changelog",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
number := utils.GetIntFlag(cmd, "number", 16)
jsonFlag := utils.OutputJSON

response, apiError := http.GetChangelog()
if !apiError.IsNil() {
utils.HandleError(apiError.Unwrap(), apiError.Message)
}

changes := models.ParseChangeLog(response, number)
printer.ChangeLog(changes, number, jsonFlag)
},
}

func init() {
changelogCmd.Flags().IntP("number", "n", 3, "number of versions to show changes for")

rootCmd.AddCommand(changelogCmd)
}
2 changes: 2 additions & 0 deletions pkg/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func installCLIUpdate() {

if wasUpdated {
utils.Log(fmt.Sprintf("Installed CLI %s", installedVersion))
utils.Log("Tip: see what's new with the 'doppler changelog' command")
utils.Log("")
} else {
utils.Log(fmt.Sprintf("You are already running the latest version"))
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/http/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,13 @@ func GetCLIInstallScript() ([]byte, Error) {
}
return resp, Error{}
}

// GetChangelog of CLI releases
func GetChangelog() ([]byte, Error) {
headers := map[string]string{"Accept": "application/json"}
_, resp, err := GetRequest("https://cli.doppler.com", true, headers, "/changes", nil)
if err != nil {
return nil, Error{Err: err, Message: "Unable to fetch changelog"}
}
return resp, Error{}
}
53 changes: 53 additions & 0 deletions pkg/models/workers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright © 2020 Doppler <support@doppler.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package models

import (
"encoding/json"

"github.com/DopplerHQ/cli/pkg/utils"
)

// ChangeLog lists changes
type ChangeLog struct {
Changes []string `json:"changes"`
}

// ParseChangeLog parse change log
func ParseChangeLog(response []byte, max int) map[string]ChangeLog {
var releaseMap []map[string]interface{}
if err := json.Unmarshal(response, &releaseMap); err != nil {
utils.HandleError(err, "Unable to parse changelog")
}

changes := map[string]ChangeLog{}

for i, release := range releaseMap {
if i >= max {
break
}

v := release["version"].(string)
var list []string
for _, change := range release["changes"].([]interface{}) {
list = append(list, change.(string))
}

changes[v] = ChangeLog{Changes: list}
}

return changes
}
26 changes: 26 additions & 0 deletions pkg/printer/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/DopplerHQ/cli/pkg/models"
"github.com/DopplerHQ/cli/pkg/utils"
"github.com/DopplerHQ/cli/pkg/version"
"github.com/jedib0t/go-pretty/table"
"github.com/jedib0t/go-pretty/text"
"gopkg.in/gookit/color.v1"
Expand Down Expand Up @@ -406,3 +407,28 @@ func ConfigServiceToken(token models.ConfigServiceToken, jsonFlag bool, plain bo
rows := [][]string{{token.Name, token.Token, token.Slug, token.Project, token.Environment, token.Config, token.CreatedAt}}
Table([]string{"name", "token", "slug", "project", "environment", "config", "created at"}, rows, TableOptions())
}

// ChangeLog print change log
func ChangeLog(changes map[string]models.ChangeLog, max int, jsonFlag bool) {
if jsonFlag {
JSON(changes)
} else {
i := 0
for v, cl := range changes {
if i >= max {
break
}
if i != 0 {
utils.Log("")
}

utils.Log(color.Cyan.Sprintf("CLI %s", version.Normalize(v)))
for _, change := range cl.Changes {
utils.Log(fmt.Sprintf("· %s", change))
}

i = i + 1
}
}

}

0 comments on commit 7c496ca

Please sign in to comment.