Skip to content

Commit

Permalink
Merge pull request #337 from IBM-Cloud/dev
Browse files Browse the repository at this point in the history
Promote 1.0.0
  • Loading branch information
Aerex authored Oct 5, 2022
2 parents 7d34e51 + 563bec6 commit 3506136
Show file tree
Hide file tree
Showing 30 changed files with 692 additions and 415 deletions.
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
#
# This line indicates we will use the hook from ibm/detect-secrets to run scan during committing phase.
# Whitewater/whitewater-detect-secrets would sync code to ibm/detect-secrets upon merge.
repos:
- repo: https://github.com/ibm/detect-secrets
# If you desire to use a specific version of detect-secrets, you can replace `master` with other git revisions such as branch, tag or commit sha.
# You are encouraged to use static refs such as tags, instead of branch name
#
# Running "pre-commit autoupdate" would automatically updates rev to latest tag
rev: master
rev: 0.13.1+ibm.50.dss
hooks:
- id: detect-secrets # pragma: whitelist secret
# Add options for detect-secrets-hook binary. You can run `detect-secrets-hook --help` to list out all possible options.
# You may also run `pre-commit run detect-secrets` to preview the scan result.
# when "--baseline" without "--use-all-plugins", pre-commit scan with just plugins in baseline file
# when "--baseline" with "--use-all-plugins", pre-commit scan with all available plugins
# add "--fail-on-non-audited" to fail pre-commit for unaudited potential secrets
args: [--baseline, .secrets.baseline, --use-all-plugins, --fail-on-non-audited]
# add "--fail-on-unaudited" to fail pre-commit for unaudited potential secrets
args: [--baseline, .secrets.baseline, --use-all-plugins, --fail-on-unaudited]
2 changes: 1 addition & 1 deletion .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
}
]
},
"version": "0.13.1+ibm.46.dss",
"version": "0.13.1+ibm.50.dss",
"word_list": {
"file": null,
"hash": null
Expand Down
7 changes: 4 additions & 3 deletions bin/generate-language-resources
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
set -e

go get github.com/jteeuwen/go-bindata/...
go-bindata -nometadata -nocompress -pkg resources -o resources/i18n_resources.go i18n/resources/*.all.json
go-bindata -nometadata -nocompress -pkg resources -o resources/i18n_resources.go i18n/resources/*.json

pushd resources
# quiet noisy pushd/popd output
pushd resources 1>/dev/null
go fmt ./...
popd
popd 1>/dev/null
12 changes: 9 additions & 3 deletions bluemix/terminal/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"github.com/mattn/go-runewidth"
)

const (
// minSpace is the number of spaces between the end of the longest value and
// the start of the next row
minSpace = 3
)

type Table interface {
Add(row ...string)
Print()
Expand Down Expand Up @@ -108,10 +114,10 @@ func (t *PrintableTable) printRow(row []string) {

func (t *PrintableTable) cellValue(col int, value string) string {
padding := ""
if col < len(t.headers)-1 {
padding = strings.Repeat(" ", t.maxSizes[col]-runewidth.StringWidth(Decolorize(value)))
if col < len(t.maxSizes)-1 {
padding = strings.Repeat(" ", t.maxSizes[col]-runewidth.StringWidth(Decolorize(value))+minSpace)
}
return fmt.Sprintf("%s%s ", value, padding)
return fmt.Sprintf("%s%s", value, padding)
}

// Prints out a nicely/human formatted Json string instead of a table structure
Expand Down
11 changes: 6 additions & 5 deletions bluemix/terminal/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package terminal_test

import (
"bytes"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"

. "github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/terminal"
)

Expand All @@ -16,7 +17,7 @@ func TestPrintTableSimple(t *testing.T) {
testTable.Print()
assert.Contains(t, buf.String(), "test2")
assert.Contains(t, buf.String(), "row1")
assert.Equal(t, buf.String(), "test1 test2 \nrow1 row2 \n")
assert.Equal(t, "test1 test2\nrow1 row2\n", buf.String())
}

func TestPrintTableJson(t *testing.T) {
Expand All @@ -36,7 +37,7 @@ func TestEmptyHeaderTable(t *testing.T) {
testTable.Add("row1", "row2")
testTable.Print()
assert.Contains(t, buf.String(), "row1")
assert.Equal(t, buf.String(), " \nrow1 row2 \n")
assert.Equal(t, " \nrow1 row2\n", buf.String())
}

func TestEmptyHeaderTableJson(t *testing.T) {
Expand All @@ -55,7 +56,7 @@ func TestZeroHeadersTable(t *testing.T) {
testTable.Add("row1", "row2")
testTable.Print()
assert.Contains(t, buf.String(), "row1")
assert.Equal(t, buf.String(), "\nrow1 row2 \n")
assert.Equal(t, "\nrow1 row2\n", buf.String())
}

func TestZeroHeadersTableJson(t *testing.T) {
Expand All @@ -77,7 +78,7 @@ func TestNotEnoughRowEntires(t *testing.T) {
testTable.Add("", "row2")
testTable.Print()
assert.Contains(t, buf.String(), "row1")
assert.Equal(t, buf.String(), "col1 col2 \nrow1 \n row2 \n")
assert.Equal(t, "col1 col2\nrow1 \n row2\n", buf.String())
}

func TestNotEnoughRowEntiresJson(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion bluemix/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package bluemix
import "fmt"

// Version is the SDK version
var Version = VersionType{Major: 0, Minor: 12, Build: 0}
var Version = VersionType{Major: 1, Minor: 0, Build: 0}

// VersionType describe version info
type VersionType struct {
Expand Down
33 changes: 25 additions & 8 deletions docs/plugin_developer_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,8 @@ You can find other examples in [tests](https://github.com/maxbrunsfeld/counterfe

IBM Cloud CLI tends to be used globally. Both IBM Cloud CLI and its plug-ins should support globalization. We have enabled internationalization (i18n) for CLI's base commands with the help of the third-party tool "[go-i18n](https://github.com/nicksnyder/go-i18n)". To keep user experience consistent, we recommend plug-in developers follow the CLI's way of i18n enablement.

Please install the *go-i18n* CLI for version 1.10.0. Newer versions of the CLI are no longer compatible with translations files prior to [email protected]. You can install the CLI using the command: `go install github.com/nicksnyder/go-i18n/[email protected]`

Here's the workflow:

1. Add new strings or replace existing strings with `T()` function calls to load the translated strings. For example:
Expand Down Expand Up @@ -1149,10 +1151,10 @@ Here's the workflow:
1. Create empty files `zh-hans.all.json` and `fr-br.all.json`, and run:
2. Run:
```bash
goi18n –outputdir <directory\_of\_generated\_translation\_files> en-us.all.json zh-hans.all.json fr-br.all.json
goi18n -flat=false –outdir <directory\_of\_generated\_translation\_files> en-us.all.json zh-hans.all.json fr-br.all.json
```

The previous command will generate 2 output files for each language: `xx-yy.all.json` contains all strings for the language, and `xx-yy.untranslated.json` contains untranslated strings. After the strings are translated, they should be merged back into `xx-yy.all.json`. For more details, refer to goi18n CLI's help by 'goi18n –help'.
The previous command will generate 2 output files for each language: `xx-yy.all.json` contains all strings for the language, and `xx-yy.untranslated.json` contains untranslated strings. After the strings are translated, they should be merged back into `xx-yy.all.json`. If plugin is on the ibm-cloud-cli-sdk 1.00 or above, rename the file from `xx-yy.all.json` to `all.xx-yy.json`. For more details, refer to goi18n CLI's help by 'goi18n –help'.
3. Package translation files. IBM Cloud CLI is to be built as a stand-alone binary distribution. In order to load i18n resource files in code, we use [go-bindata](https://github.com/jteeuwen/go-bindata) to auto-generate Go source code from all i18n resource files and the compile them into the binary. You can write a script to do it automatically during build. A sample script could be like:
Expand All @@ -1174,34 +1176,49 @@ Here's the workflow:
`T()` must be initialized before use. During i18n initialization in IBM Cloud CLI, user locale is used if it's set in `~/.bluemix/config.json` (plug-in can get user locale via `PluginContext.Locale()`). Otherwise, system locale is auto discovered (see [jibber\_jabber](https://github.com/cloudfoundry/jibber_jabber)) and used. If system locale is not detected or supported, default locale `en\_US` is then used. Next, we initialize the translate function with the locale. Sample code:

```go
func initWithLocale(locale string) goi18n.TranslateFunc {
import (
"fmt"
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/i18n"
)
var T i18n.TranslateFunc = Init(core_config.NewCoreConfig(func(e error) {}), new(JibberJabberDetector))
func Init(coreConfig core_config.Repository, detector Detector) i18n.TranslateFunc {
bundle = i18n.Bundle()
userLocale := coreConfig.Locale()
if userLocale != "" {
return initWithLocale(userLocale)
}
}
func initWithLocale(locale string) i18n.TranslateFunc {
err := loadFromAsset(locale)
if err != nil {
panic(err)
}
return goi18n.MustTfunc(locale, DEFAULT_LOCALE)
return i18n.MustTfunc(locale, DEFAULT_LOCALE)
}
// load translation asset for the given locale
func loadFromAsset(locale string) (err error) {
assetName := locale + ".all.json"
assetName := fmt.Sprintf("all.%s.json", locale)
assetKey := filepath.Join(resourcePath, assetName)
bytes, err := resources.Asset(assetKey)
if err != nil {
return
}
err = goi18n.ParseTranslationFileBytes(assetName, bytes)
_, err = bundle.ParseMessageFileBytes(bytes, resourceKey)
return
}
```

## 8. Command Design

### 8.1. Honour Region/Resource Group Setting of CLI
### 8.1. Honor Region/Resource Group Setting of CLI

When users are using CLI, they probably have already targeted region or resource group during login. It's cumbersome to ask users to re-target region or resource group in specific command again.
- By default, plugin should honour the region/resource group setting of CLI. Check `CurrentRegion`, `HasTargetedRegion`, `CurrentResourceGroup`, and `HasTargetedResourceGroup` in the [`core_config.Repository`](https://godoc.org/github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/configuration/core_config#Repository).
- By default, plugin should honor the region/resource group setting of CLI. Check `CurrentRegion`, `HasTargetedRegion`, `CurrentResourceGroup`, and `HasTargetedResourceGroup` in the [`core_config.Repository`](https://godoc.org/github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/configuration/core_config#Repository).
```go
func (demo *DemoPlugin) Run(context plugin.PluginContext, args []string){
Expand Down
20 changes: 14 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@ require (
github.com/fatih/structs v1.0.1-0.20171020064819-f5faa72e7309
github.com/mattn/go-colorable v0.0.0-20160210001857-9fdad7c47650
github.com/mattn/go-runewidth v0.0.0-20151118072159-d96d1bd051f2
github.com/nicksnyder/go-i18n v1.3.0
github.com/onsi/ginkgo v1.3.1
github.com/onsi/gomega v1.1.0
github.com/nicksnyder/go-i18n/v2 v2.2.0
github.com/onsi/ginkgo v1.6.0
github.com/onsi/gomega v1.10.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.3
github.com/stretchr/testify v1.2.2
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e
golang.org/x/text v0.3.7
gopkg.in/cheggaaa/pb.v1 v1.0.15
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hpcloud/tail v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-isatty v0.0.5-0.20180830101745-3fb116b82035 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
)
Loading

0 comments on commit 3506136

Please sign in to comment.