-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add clear cmd & fix issue#11 #15 & add utest * update docs * fix for golang-ci * fix golang style * fix utest
- Loading branch information
Showing
21 changed files
with
519 additions
and
24 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,75 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"reflect" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
// ClearCommand clean command struct | ||
type ClearCommand struct { | ||
BaseCommand | ||
} | ||
|
||
// Init AliasCommand | ||
func (cl *ClearCommand) Init() { | ||
cl.command = &cobra.Command{ | ||
Use: "clear", | ||
Short: "Clear lapsed context, cluster and user", | ||
Long: "Clear lapsed context, cluster and user", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return cl.runClear(cmd, args) | ||
}, | ||
Example: clearExample(), | ||
} | ||
cl.command.DisableFlagsInUseLine = true | ||
} | ||
|
||
func (cl *ClearCommand) runClear(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
if ok, err := clearContext(cfgFile); ok { | ||
printString(os.Stdout, fmt.Sprintf("There is nothing to clean in 「%s」\n", cfgFile)) | ||
return nil | ||
} else if err != nil { | ||
return err | ||
} | ||
} else { | ||
for _, file := range args { | ||
if ok, err := clearContext(file); ok { | ||
printString(os.Stdout, fmt.Sprintf("There is nothing to clean in 「%s」\n", file)) | ||
} else if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func clearContext(file string) (bool, error) { | ||
config, err := clientcmd.LoadFromFile(file) | ||
if err != nil { | ||
return false, err | ||
} | ||
outConfig := config.DeepCopy() | ||
outConfig = CheckValidContext(outConfig) | ||
if reflect.DeepEqual(config, outConfig) { | ||
return true, nil | ||
} | ||
err = UpdateConfigFile(file, outConfig) | ||
if err != nil { | ||
return false, err | ||
} | ||
return false, nil | ||
} | ||
|
||
func clearExample() string { | ||
return fmt.Sprintf(` | ||
# Clear lapsed context, cluster and user (default is %s) | ||
kubecm clear | ||
# Customised clear lapsed files | ||
kubecm clear config.yaml test.yaml | ||
`, cfgFile) | ||
} |
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,44 @@ | ||
package cmd | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
func Test_clearContext(t *testing.T) { | ||
trueFile, _ := ioutil.TempFile("", "") | ||
falseFile, _ := ioutil.TempFile("", "") | ||
defer os.Remove(trueFile.Name()) | ||
defer os.Remove(falseFile.Name()) | ||
_ = clientcmd.WriteToFile(appendMergeConfig, trueFile.Name()) | ||
_ = clientcmd.WriteToFile(wrongRootConfig, falseFile.Name()) | ||
|
||
type args struct { | ||
file string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
{"true", args{file: trueFile.Name()}, true, false}, | ||
{"false", args{file: falseFile.Name()}, false, false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := clearContext(tt.args.file) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("clearContext() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("clearContext() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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
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,72 @@ | ||
package cmd | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api" | ||
) | ||
|
||
var ( | ||
noRootMergeConfig = clientcmdapi.Config{ | ||
AuthInfos: map[string]*clientcmdapi.AuthInfo{ | ||
"black-user": {Token: "black-token"}, | ||
"red-user": {Token: "red-token"}, | ||
}, | ||
Clusters: map[string]*clientcmdapi.Cluster{ | ||
"pig-cluster": {Server: "http://pig.org:8080"}, | ||
"cow-cluster": {Server: "http://cow.org:8080"}, | ||
}, | ||
Contexts: map[string]*clientcmdapi.Context{ | ||
"federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"}, | ||
}, | ||
} | ||
noFederalMergeConfig = clientcmdapi.Config{ | ||
AuthInfos: map[string]*clientcmdapi.AuthInfo{ | ||
"black-user": {Token: "black-token"}, | ||
"red-user": {Token: "red-token"}, | ||
}, | ||
Clusters: map[string]*clientcmdapi.Cluster{ | ||
"pig-cluster": {Server: "http://pig.org:8080"}, | ||
"cow-cluster": {Server: "http://cow.org:8080"}, | ||
}, | ||
Contexts: map[string]*clientcmdapi.Context{ | ||
"root-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}, | ||
}, | ||
} | ||
) | ||
|
||
func NewAllConfig() *clientcmdapi.Config { | ||
return appendMergeConfig.DeepCopy() | ||
} | ||
|
||
func Test_filterArgs(t *testing.T) { | ||
|
||
type args struct { | ||
args []string | ||
config *clientcmdapi.Config | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *clientcmdapi.Config | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
{"noFederal", args{[]string{"root"}, NewAllConfig()}, &noFederalMergeConfig, false}, | ||
{"noRoot", args{[]string{"federal"}, NewAllConfig()}, &noRootMergeConfig, false}, | ||
{"all-context", args{[]string{"root", "federal"}, NewAllConfig()}, &appendMergeConfig, false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := filterArgs(tt.args.args, tt.args.config) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("filterArgs() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("filterArgs() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.