-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
152 additions
and
25 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,88 @@ | ||
/* | ||
Copyright © 2019 NAME HERE <EMAIL ADDRESS> | ||
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 ( | ||
"crypto/sha256" | ||
"encoding/json" | ||
"fmt" | ||
"reflect" | ||
"sort" | ||
) | ||
|
||
// SortArrayAndComputeHash sorts a string array and | ||
// returns a hash for it | ||
func SortArrayAndComputeHash(s []string) (string, error) { | ||
sort.Strings(s) | ||
data, err := json.Marshal(s) | ||
if err != nil { | ||
return "", err | ||
} | ||
return Encode(Hash(string(data))) | ||
} | ||
|
||
// Copied from https://github.com/kubernetes/kubernetes | ||
// /blob/master/pkg/kubectl/util/hash/hash.go | ||
func Encode(hex string) (string, error) { | ||
if len(hex) < 10 { | ||
return "", fmt.Errorf( | ||
"input length must be at least 10") | ||
} | ||
enc := []rune(hex[:10]) | ||
for i := range enc { | ||
switch enc[i] { | ||
case '0': | ||
enc[i] = 'g' | ||
case '1': | ||
enc[i] = 'h' | ||
case '3': | ||
enc[i] = 'k' | ||
case 'a': | ||
enc[i] = 'm' | ||
case 'e': | ||
enc[i] = 't' | ||
} | ||
} | ||
return string(enc), nil | ||
} | ||
|
||
// Hash returns the hex form of the sha256 of the argument. | ||
func Hash(data string) string { | ||
return fmt.Sprintf("%x", sha256.Sum256([]byte(data))) | ||
} | ||
|
||
// Struct2Map | ||
func Struct2Map(obj interface{}) map[string]interface{} { | ||
t := reflect.TypeOf(obj) | ||
v := reflect.ValueOf(obj) | ||
|
||
var data = make(map[string]interface{}) | ||
for i := 0; i < t.NumField(); i++ { | ||
data[t.Field(i).Name] = v.Field(i).Interface() | ||
} | ||
return data | ||
} | ||
|
||
// HashSuffix return the string of kubeconfig. | ||
func HashSuffix(a Config) string { | ||
tmp := Struct2Map(a) | ||
str, err := json.Marshal(tmp) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
sum, _ := Encode(Hash(string(str))) | ||
return sum | ||
} |
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