diff --git a/Makefile b/Makefile index d040c4d..23241bf 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,21 @@ -all: build +all: lint build test build: - go install ${SRC} + go build -x ./ -clean: - go clean -i ${SRC} +install: + go install -x ${SRC} + +lint: + gometalinter --exclude=vendor --exclude=repos --disable-all --enable=golint --enable=vet --enable=gofmt ./... + find . -name '*.go' -not -path "./vendor/*" | xargs gofmt -w -s test: go test ${SRC} + +misspell: + find . -name '*.go' -not -path './vendor/*' -not -path './_repos/*' | xargs misspell -error + +clean: + go clean -x -i ${SRC} + diff --git a/api/logs.go b/api/logs.go index 9ce4b5a..2731b4b 100644 --- a/api/logs.go +++ b/api/logs.go @@ -32,7 +32,7 @@ func (x *recentLogs) Execute(args []string) error { return nil } -// GetOPLogs +// GetOPLogs ... // // e.g. curl -X GET --header 'Accept: application/json' 'https://11.11.11.12/api/logs?username=admin&repository=prj2%2Fphoton&tag=v3&operation=push&begin_timestamp=20171102&page=1&page_size=10' func GetOPLogs(baseURL string) { diff --git a/api/projects.go b/api/projects.go index 9f4042c..5827738 100644 --- a/api/projects.go +++ b/api/projects.go @@ -14,7 +14,7 @@ func init() { "This endpoint is for user to create a new project.", &prjCreate) utils.Parser.AddCommand("prj_get", - "Return specific project detail infomation.", + "Return specific project detail information.", "This endpoint returns specific project information by project ID.", &prjGet) utils.Parser.AddCommand("prj_del", diff --git a/api/targets.go b/api/targets.go index 853bf03..01ccc03 100644 --- a/api/targets.go +++ b/api/targets.go @@ -84,7 +84,7 @@ func (x *targetsPing) Execute(args []string) error { } type targetsPingByID struct { - Id int `short:"i" long:"id" description:"(REQUIRED) The replication's target ID." required:"yes"` + ID int `short:"i" long:"id" description:"(REQUIRED) The replication's target ID." required:"yes"` } var tpingByID targetsPingByID @@ -95,7 +95,7 @@ func (x *targetsPingByID) Execute(args []string) error { } type targetsDeleteByID struct { - Id int `short:"i" long:"id" description:"(REQUIRED) The replication's target ID." required:"yes"` + ID int `short:"i" long:"id" description:"(REQUIRED) The replication's target ID." required:"yes"` } var tdByID targetsDeleteByID @@ -106,7 +106,7 @@ func (x *targetsDeleteByID) Execute(args []string) error { } type targetsGetByID struct { - Id int `short:"i" long:"id" description:"(REQUIRED) The replication's target ID." required:"yes"` + ID int `short:"i" long:"id" description:"(REQUIRED) The replication's target ID." required:"yes"` } var tgByID targetsGetByID @@ -117,7 +117,7 @@ func (x *targetsGetByID) Execute(args []string) error { } type targetsUpdateByID struct { - Id int `short:"i" long:"id" description:"(REQUIRED) The replication's target ID." required:"yes" json:"-"` + ID int `short:"i" long:"id" description:"(REQUIRED) The replication's target ID." required:"yes" json:"-"` EndpointURL string `short:"e" long:"endpoint" description:"(REQUIRED) The target address URL string." required:"yes" json:"endpoint"` EndpointName string `short:"n" long:"name" description:"(REQUIRED) The target name." required:"yes" json:"name"` Username string `short:"u" long:"username" description:"(REQUIRED) The target server username." required:"yes" json:"username"` @@ -133,7 +133,7 @@ func (x *targetsUpdateByID) Execute(args []string) error { } type targetsPoliciesByID struct { - Id int `short:"i" long:"id" description:"(REQUIRED) The replication's target ID." required:"yes"` + ID int `short:"i" long:"id" description:"(REQUIRED) The replication's target ID." required:"yes"` } var tpoliciesByID targetsPoliciesByID @@ -254,7 +254,7 @@ func PostTargetsPing(baseURL string) { // // e.g. curl -X POST --header 'Content-Type: application/json' --header 'Accept: text/plain' 'https://11.11.11.12/api/targets/1/ping' func PostTargetsPingByID(baseURL string) { - targetURL := baseURL + "/" + strconv.Itoa(tpingByID.Id) + "/ping" + targetURL := baseURL + "/" + strconv.Itoa(tpingByID.ID) + "/ping" fmt.Println("==> POST", targetURL) // Read beegosessionID from .cookie.yaml @@ -276,7 +276,7 @@ func PostTargetsPingByID(baseURL string) { // // e.g. curl -X DELETE --header 'Accept: text/plain' 'https://11.11.11.12/api/targets/2' func DeleteTargetsByID(baseURL string) { - targetURL := baseURL + "/" + strconv.Itoa(tdByID.Id) + targetURL := baseURL + "/" + strconv.Itoa(tdByID.ID) fmt.Println("==> DELETE", targetURL) // Read beegosessionID from .cookie.yaml @@ -298,7 +298,7 @@ func DeleteTargetsByID(baseURL string) { // // e.g. curl -X GET --header 'Accept: application/json' 'https://11.11.11.12/api/targets/1' func GetTargetsByID(baseURL string) { - targetURL := baseURL + "/" + strconv.Itoa(tgByID.Id) + targetURL := baseURL + "/" + strconv.Itoa(tgByID.ID) fmt.Println("==> GET", targetURL) // Read beegosessionID from .cookie.yaml @@ -334,7 +334,7 @@ curl -X PUT --header 'Content-Type: text/plain' --header 'Accept: text/plain' -d }' 'https://11.11.11.12/api/targets/4' */ func UpdateTargetsByID(baseURL string) { - targetURL := baseURL + "/" + strconv.Itoa(tuByID.Id) + targetURL := baseURL + "/" + strconv.Itoa(tuByID.ID) fmt.Println("==> PUT", targetURL) // Read beegosessionID from .cookie.yaml @@ -365,7 +365,7 @@ func UpdateTargetsByID(baseURL string) { // // e.g. curl -X GET --header 'Accept: application/json' 'https://11.11.11.12/api/targets/1/policies/' func GetPoliciesByID(baseURL string) { - targetURL := baseURL + "/" + strconv.Itoa(tpoliciesByID.Id) + "/policies/" + targetURL := baseURL + "/" + strconv.Itoa(tpoliciesByID.ID) + "/policies/" fmt.Println("==> GET", targetURL) // Read beegosessionID from .cookie.yaml diff --git a/utils/heapsort.go b/utils/heapsort.go index ae01806..9926d05 100644 --- a/utils/heapsort.go +++ b/utils/heapsort.go @@ -70,14 +70,14 @@ func (h *minheap) Pop() interface{} { // Sort on the score of repos var minh = minheap{} -var mh_bk = minheap{} +var mhBk = minheap{} func example1() { h := minheap{ &repoItem{ data: &repoTop{ - Id: 31, + ID: 31, Name: "prj2/photon", ProjectID: 3, Description: "", @@ -91,7 +91,7 @@ func example1() { }, &repoItem{ data: &repoTop{ - Id: 30, + ID: 30, Name: "sf3prj/hello-world", ProjectID: 13, Description: "", @@ -105,7 +105,7 @@ func example1() { }, &repoItem{ data: &repoTop{ - Id: 29, + ID: 29, Name: "prj5/hello-world", ProjectID: 6, Description: "", @@ -119,7 +119,7 @@ func example1() { }, &repoItem{ data: &repoTop{ - Id: 4, + ID: 4, Name: "prj3/hello-world", ProjectID: 4, Description: "", diff --git a/utils/retention_policy.go b/utils/retention_policy.go index 0493255..97b2823 100644 --- a/utils/retention_policy.go +++ b/utils/retention_policy.go @@ -28,7 +28,7 @@ var stats statistics // --- type repoTop struct { - Id int `json:"id"` + ID int `json:"id"` Name string `json:"name"` ProjectID int `json:"project_id"` Description string `json:"description"` @@ -92,10 +92,10 @@ type reposRetentionPolicy struct { var reposRP reposRetentionPolicy func (x *reposRetentionPolicy) Execute(args []string) error { - if err := repo_analyse(); err != nil { + if err := repoAnalyse(); err != nil { os.Exit(1) } - if err := repo_erase(); err != nil { + if err := repoErase(); err != nil { os.Exit(1) } rpGCHint() @@ -110,13 +110,13 @@ type tagsRetentionPolicy struct { var tagsRP tagsRetentionPolicy func (x *tagsRetentionPolicy) Execute(args []string) error { - if err := tag_analyse_and_erase(); err != nil { + if err := tagAnalyseAndErase(); err != nil { os.Exit(1) } return nil } -func tag_analyse_and_erase() error { +func tagAnalyseAndErase() error { c, err := CookieLoad() if err != nil { @@ -327,13 +327,13 @@ func grade(r *repoTop, rp *retentionPolicy) float32 { score := rp.UpdateTime.Base*uf + rp.PullCount.Base*pf + rp.TagsCount.Base*tf fmt.Printf("[factors] ==> score = UpdateTimeBase*uf + PullCountBase*pf + TagsCountBase*tf = %.2f * %.2f + %.2f * %.2f + %.2f * %.2f = %.2f repo_id: %d\n", - rp.UpdateTime.Base, uf, rp.PullCount.Base, pf, rp.TagsCount.Base, tf, score, r.Id) + rp.UpdateTime.Base, uf, rp.PullCount.Base, pf, rp.TagsCount.Base, tf, score, r.ID) return score } -// repo_analyse calculates scores and output topN element by minheap sort -func repo_analyse() error { +// repoAnalyse calculates scores and output topN element by minheap sort +func repoAnalyse() error { rp, err := rpLoad() if err != nil { @@ -378,7 +378,7 @@ func repo_analyse() error { } heap.Init(&minh) - heap.Init(&mh_bk) + heap.Init(&mhBk) for _, r := range repos { sc := grade(r, rp) @@ -400,21 +400,21 @@ func repo_analyse() error { score: sc, } heap.Push(&minh, it) - heap.Push(&mh_bk, it) + heap.Push(&mhBk, it) } fmt.Printf("\n========== 根据分数排名(由低到高)建议删除 public repo 信息如下 ========\n\n") - for mh_bk.Len() > 0 { - it := heap.Pop(&mh_bk).(*repoItem) + for mhBk.Len() > 0 { + it := heap.Pop(&mhBk).(*repoItem) fmt.Printf("%.2f <==> %+v\n", it.score, *it.data) } return nil } -// repo_erase implements soft deletion -func repo_erase() error { +// repoErase implements soft deletion +func repoErase() error { var num int scanner := bufio.NewScanner(os.Stdin) @@ -450,7 +450,7 @@ func repo_erase() error { if num <= 0 || num > 50 { fmt.Println("[Warning] The valid number range is (0, 50].") fmt.Println("[Warning] Sorry, you're not allowed proceeding... Abort.") - return fmt.Errorf("error: The number is Out of Range.") + return fmt.Errorf("error: the number is out of range") } fmt.Printf("\n=== 开始 soft deletion ===\n\n") @@ -476,7 +476,7 @@ func repo_erase() error { Set("Cookie", "harbor-lang=zh-cn; beegosessionID="+c.BeegosessionID). End(PrintStatus) } - num -= 1 + num-- } fmt.Printf("\n=== 完成 soft deletion ===\n\n") diff --git a/utils/utils.go b/utils/utils.go index a0c724d..4dc9868 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -14,8 +14,8 @@ import ( yaml "gopkg.in/yaml.v2" ) -var errMalCookies = errors.New("Get Malformed Cookies.") -var errCookiesNotAvailable = errors.New("Target Cookies are Not Available.") +var errMalCookies = errors.New("get malformed cookies") +var errCookiesNotAvailable = errors.New("target cookies are not available") // Parser is a command registry var Parser = flags.NewParser(nil, flags.Default) @@ -27,7 +27,8 @@ var Request = gorequest.New().TLSClientConfig(&tls.Config{InsecureSkipVerify: tr var configfile = "./config.yaml" var secretfile = "./.cookie.yaml" -type beegocookie struct { +// Beegocookie is for beegosessionID storage +type Beegocookie struct { BeegosessionID string `yaml:"beegosessionID"` } @@ -36,7 +37,8 @@ type generalConfig struct { Dstip string `yaml:"dstip"` } -type sysConfig struct { +// SysConfig defines system configurations +type SysConfig struct { AuthMode string `yaml:"auth_mode" json:"auth_mode"` EmailFrom string `yaml:"email_from" json:"email_from"` EmailHost string `yaml:"email_host" json:"email_host"` @@ -45,11 +47,11 @@ type sysConfig struct { EmailUsername string `yaml:"email_username" json:"email_username"` EmailSsl bool `yaml:"email_ssl" json:"email_ssl"` EmailInsecure bool `yaml:"email_insecure" json:"email_insecure"` - LdapUrl string `yaml:"ldap_url" json:"ldap_url"` + LdapURL string `yaml:"ldap_url" json:"ldap_url"` LdapBaseDN string `yaml:"ldap_base_dn" json:"ldap_base_dn"` LdapFilter string `yaml:"ldap_filter" json:"ldap_filter"` LdapScope int `yaml:"ldap_scope" json:"ldap_scope"` - LdapUid string `yaml:"ldap_uid" jsonb:"ldap_uid"` + LdapUID string `yaml:"ldap_uid" jsonb:"ldap_uid"` LdapSearchDN string `yaml:"ldap_search_dn" json:"ldap_search_dn"` LdapTimeout int `yaml:"ldap_timeout" json:"ldap_timeout"` ProjectCreationRestriction string `yaml:"project_creation_restriction" json:"project_creation_restriction"` @@ -99,7 +101,7 @@ func cookieFilter(cookies []*http.Cookie, filter string) (string, error) { // .cookie.yaml no matter whether it exists or not. func cookieSave(beegosessionID string) error { - var cookie beegocookie + var cookie Beegocookie cookie.BeegosessionID = beegosessionID c, err := yaml.Marshal(&cookie) @@ -116,8 +118,8 @@ func cookieSave(beegosessionID string) error { } // CookieLoad loads beegosessionID from .cookie.yaml. -func CookieLoad() (*beegocookie, error) { - var cookie beegocookie +func CookieLoad() (*Beegocookie, error) { + var cookie Beegocookie dataBytes, err := ioutil.ReadFile(secretfile) if err != nil { @@ -132,8 +134,8 @@ func CookieLoad() (*beegocookie, error) { } // SysConfigLoad loads system configuration from config.yaml. -func SysConfigLoad() (*sysConfig, error) { - var config sysConfig +func SysConfigLoad() (*SysConfig, error) { + var config SysConfig dataBytes, err := ioutil.ReadFile(configfile) if err != nil { diff --git a/vendor/github.com/jessevdk/go-flags/completion.go b/vendor/github.com/jessevdk/go-flags/completion.go index 7a7a08b..37406fc 100644 --- a/vendor/github.com/jessevdk/go-flags/completion.go +++ b/vendor/github.com/jessevdk/go-flags/completion.go @@ -76,7 +76,7 @@ func (c *completion) skipPositional(s *parseState, n int) { func (c *completion) completeOptionNames(s *parseState, prefix string, match string, short bool) []Completion { if short && len(match) != 0 { return []Completion{ - Completion{ + { Item: prefix + match, }, } diff --git a/vendor/github.com/moul/http2curl/vendor/github.com/jtolds/gls/context.go b/vendor/github.com/moul/http2curl/vendor/github.com/jtolds/gls/context.go index 94d7fbb..68d4056 100644 --- a/vendor/github.com/moul/http2curl/vendor/github.com/jtolds/gls/context.go +++ b/vendor/github.com/moul/http2curl/vendor/github.com/jtolds/gls/context.go @@ -137,7 +137,7 @@ func Go(cb func()) { mgrRegistryMtx.RLock() defer mgrRegistryMtx.RUnlock() - for mgr, _ := range mgrRegistry { + for mgr := range mgrRegistry { values := mgr.getValues() if len(values) > 0 { mgr_copy := mgr diff --git a/vendor/github.com/moul/http2curl/vendor/github.com/smartystreets/assertions/internal/oglemock/controller.go b/vendor/github.com/moul/http2curl/vendor/github.com/smartystreets/assertions/internal/oglemock/controller.go index 93a1d62..cfcca03 100644 --- a/vendor/github.com/moul/http2curl/vendor/github.com/smartystreets/assertions/internal/oglemock/controller.go +++ b/vendor/github.com/moul/http2curl/vendor/github.com/smartystreets/assertions/internal/oglemock/controller.go @@ -290,7 +290,7 @@ func (c *controllerImpl) chooseExpectationLocked( func makeZeroReturnValues(signature reflect.Type) []interface{} { result := make([]interface{}, signature.NumOut()) - for i, _ := range result { + for i := range result { outType := signature.Out(i) zeroVal := reflect.Zero(outType) result[i] = zeroVal.Interface() diff --git a/vendor/gopkg.in/yaml.v2/decode_test.go b/vendor/gopkg.in/yaml.v2/decode_test.go index 9269f12..2baa4b2 100644 --- a/vendor/gopkg.in/yaml.v2/decode_test.go +++ b/vendor/gopkg.in/yaml.v2/decode_test.go @@ -163,13 +163,13 @@ var unmarshalTests = []struct { map[string]interface{}{"seq": []interface{}{"A", "B"}}, }, { "seq: [A,B,C,]", - map[string][]string{"seq": []string{"A", "B", "C"}}, + map[string][]string{"seq": {"A", "B", "C"}}, }, { "seq: [A,1,C]", - map[string][]string{"seq": []string{"A", "1", "C"}}, + map[string][]string{"seq": {"A", "1", "C"}}, }, { "seq: [A,1,C]", - map[string][]int{"seq": []int{1}}, + map[string][]int{"seq": {1}}, }, { "seq: [A,1,C]", map[string]interface{}{"seq": []interface{}{"A", 1, "C"}}, @@ -180,13 +180,13 @@ var unmarshalTests = []struct { map[string]interface{}{"seq": []interface{}{"A", "B"}}, }, { "seq:\n - A\n - B\n - C", - map[string][]string{"seq": []string{"A", "B", "C"}}, + map[string][]string{"seq": {"A", "B", "C"}}, }, { "seq:\n - A\n - 1\n - C", - map[string][]string{"seq": []string{"A", "1", "C"}}, + map[string][]string{"seq": {"A", "1", "C"}}, }, { "seq:\n - A\n - 1\n - C", - map[string][]int{"seq": []int{1}}, + map[string][]int{"seq": {1}}, }, { "seq:\n - A\n - 1\n - C", map[string]interface{}{"seq": []interface{}{"A", 1, "C"}}, @@ -398,7 +398,7 @@ var unmarshalTests = []struct { map[interface{}]interface{}{"1": "\"2\""}, }, { "v:\n- A\n- 'B\n\n C'\n", - map[string][]string{"v": []string{"A", "B\nC"}}, + map[string][]string{"v": {"A", "B\nC"}}, }, // Explicit tags. @@ -587,11 +587,11 @@ var unmarshalTests = []struct { // Support encoding.TextUnmarshaler. { "a: 1.2.3.4\n", - map[string]textUnmarshaler{"a": textUnmarshaler{S: "1.2.3.4"}}, + map[string]textUnmarshaler{"a": {S: "1.2.3.4"}}, }, { "a: 2015-02-24T18:19:39Z\n", - map[string]textUnmarshaler{"a": textUnmarshaler{"2015-02-24T18:19:39Z"}}, + map[string]textUnmarshaler{"a": {"2015-02-24T18:19:39Z"}}, }, // Timestamps diff --git a/vendor/gopkg.in/yaml.v2/encode_test.go b/vendor/gopkg.in/yaml.v2/encode_test.go index f0911a7..c03f1a4 100644 --- a/vendor/gopkg.in/yaml.v2/encode_test.go +++ b/vendor/gopkg.in/yaml.v2/encode_test.go @@ -97,13 +97,13 @@ var marshalTests = []struct { map[string]interface{}{"v": ""}, "v: \"\"\n", }, { - map[string][]string{"v": []string{"A", "B"}}, + map[string][]string{"v": {"A", "B"}}, "v:\n- A\n- B\n", }, { - map[string][]string{"v": []string{"A", "B\nC"}}, + map[string][]string{"v": {"A", "B\nC"}}, "v:\n- A\n- |-\n B\n C\n", }, { - map[string][]interface{}{"v": []interface{}{"A", 1, map[string][]int{"B": []int{2, 3}}}}, + map[string][]interface{}{"v": {"A", 1, map[string][]int{"B": {2, 3}}}}, "v:\n- A\n- 1\n- B:\n - 2\n - 3\n", }, { map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}}, diff --git a/vendor/gopkg.in/yaml.v2/readerc.go b/vendor/gopkg.in/yaml.v2/readerc.go index 7c1f5fa..b0c436c 100644 --- a/vendor/gopkg.in/yaml.v2/readerc.go +++ b/vendor/gopkg.in/yaml.v2/readerc.go @@ -95,7 +95,7 @@ func yaml_parser_update_buffer(parser *yaml_parser_t, length int) bool { // [Go] This function was changed to guarantee the requested length size at EOF. // The fact we need to do this is pretty awful, but the description above implies - // for that to be the case, and there are tests + // for that to be the case, and there are tests // If the EOF flag is set and the raw buffer is empty, do nothing. if parser.eof && parser.raw_buffer_pos == len(parser.raw_buffer) { diff --git a/vendor/gopkg.in/yaml.v2/resolve.go b/vendor/gopkg.in/yaml.v2/resolve.go index 6c151db..bf830ee 100644 --- a/vendor/gopkg.in/yaml.v2/resolve.go +++ b/vendor/gopkg.in/yaml.v2/resolve.go @@ -180,7 +180,7 @@ func resolve(tag string, in string) (rtag string, out interface{}) { return yaml_INT_TAG, uintv } } else if strings.HasPrefix(plain, "-0b") { - intv, err := strconv.ParseInt("-" + plain[3:], 2, 64) + intv, err := strconv.ParseInt("-"+plain[3:], 2, 64) if err == nil { if true || intv == int64(int(intv)) { return yaml_INT_TAG, int(intv) diff --git a/vendor/gopkg.in/yaml.v2/sorter.go b/vendor/gopkg.in/yaml.v2/sorter.go index 4c45e66..2edd734 100644 --- a/vendor/gopkg.in/yaml.v2/sorter.go +++ b/vendor/gopkg.in/yaml.v2/sorter.go @@ -52,7 +52,7 @@ func (l keyList) Less(i, j int) bool { var ai, bi int var an, bn int64 if ar[i] == '0' || br[i] == '0' { - for j := i-1; j >= 0 && unicode.IsDigit(ar[j]); j-- { + for j := i - 1; j >= 0 && unicode.IsDigit(ar[j]); j-- { if ar[j] != '0' { an = 1 bn = 1