Skip to content

Commit dfba8e1

Browse files
committed
update
1 parent 3903dd2 commit dfba8e1

File tree

9 files changed

+219
-23
lines changed

9 files changed

+219
-23
lines changed

Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
SHELL := /bin/bash
2+
GO := GO111MODULE=on go
3+
REV := $(shell git rev-parse --short HEAD 2> /dev/null || echo 'unknown')
4+
GO_VERSION := $(shell $(GO) version | sed -e 's/^[^0-9.]*\([0-9.]*\).*/\1/')
5+
GO_DEPENDENCIES := $(call rwildcard,pkg/,*.go) $(call rwildcard,cmd/,*.go)
6+
7+
BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2> /dev/null || echo 'unknown')
8+
BUILD_DATE := $(shell date +%Y%m%d-%H:%M:%S)
9+
CGO_ENABLED = 0
10+
11+
12+
GOTEST := $(GO) test
13+
VERSION ?= $(shell echo "$$(git for-each-ref refs/tags/ --count=1 --sort=-version:refname --format='%(refname:short)' 2>/dev/null)-dev+$(REV)" | sed 's/^v//')
14+
DIRNAME := $(shell basename $(shell pwd))
15+
BUILD_TIME_CONFIG_FLAGS ?= ""
16+
17+
BUILDFLAGS := -ldflags \
18+
"-X github.com/hacker65536/${DIRNAME}/cmd.GitCommit=$(REV) \
19+
-X github.com/hacker65536/${DIRNAME}/cmd.Version=$(VERSION) \
20+
$(BUILD_TIME_CONFIG_FLAGS)"
21+
22+
23+
24+
version:
25+
@echo ${VERSION}
26+
install: $(GO_DEPENDENCIES) ## Install the binary
27+
GOBIN=${GOPATH}/bin $(GO) install $(BUILDFLAGS)
28+
29+
30+
# https://sourcegraph.com/github.com/jenkins-x/jx/-/blob/Makefile?L17

cmd/listenerRule.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package cmd
1818
import (
1919
"fmt"
2020

21+
"github.com/hacker65536/openAWS/pkg/myaws"
2122
"github.com/spf13/cobra"
22-
"github.com/hacker65536/openAWS/pkg/awselbv2"
2323
)
2424

2525
// listenerRuleCmd represents the listenerRule command
@@ -33,8 +33,8 @@ Cobra is a CLI library for Go that empowers applications.
3333
This application is a tool to generate the needed files
3434
to quickly create a Cobra application.`,
3535
Run: func(cmd *cobra.Command, args []string) {
36-
//fmt.Println("listenerRule called")
37-
a := awselbv2.ListeneRule(args[0])
36+
// fmt.Println("listenerRule called")
37+
a := myaws.ListeneRule(args[0])
3838

3939
fmt.Println(a)
4040
},

cmd/s3.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"fmt"
20+
21+
"github.com/hacker65536/openAWS/pkg/myaws"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
// s3Cmd represents the s3 command
26+
var s3Cmd = &cobra.Command{
27+
Use: "s3",
28+
Short: "A brief description of your command",
29+
Long: `A longer description that spans multiple lines and likely contains examples
30+
and usage of using your command. For example:
31+
32+
Cobra is a CLI library for Go that empowers applications.
33+
This application is a tool to generate the needed files
34+
to quickly create a Cobra application.`,
35+
Run: func(cmd *cobra.Command, args []string) {
36+
fmt.Println(myaws.OpenS3(args[0]))
37+
},
38+
}
39+
40+
func init() {
41+
rootCmd.AddCommand(s3Cmd)
42+
43+
// Here you will define your flags and configuration settings.
44+
45+
// Cobra supports Persistent Flags which will work for this command
46+
// and all subcommands, e.g.:
47+
// s3Cmd.PersistentFlags().String("foo", "", "A help for foo")
48+
49+
// Cobra supports local flags which will only run when this command
50+
// is called directly, e.g.:
51+
// s3Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
52+
}

cmd/version.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"fmt"
20+
21+
log "github.com/sirupsen/logrus"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
var (
26+
GitCommit string
27+
Version string
28+
)
29+
30+
// versionCmd represents the version command
31+
var versionCmd = &cobra.Command{
32+
Use: "version",
33+
Short: "A brief description of your command",
34+
Long: `A longer description that spans multiple lines and likely contains examples
35+
and usage of using your command. For example:
36+
37+
Cobra is a CLI library for Go that empowers applications.
38+
This application is a tool to generate the needed files
39+
to quickly create a Cobra application.`,
40+
Run: func(cmd *cobra.Command, args []string) {
41+
fmt.Printf("version: %s-%s\n", Version, GitCommit)
42+
43+
log.WithFields(
44+
log.Fields{
45+
"version": Version,
46+
"GitCommit": GitCommit,
47+
}).Debug()
48+
},
49+
}
50+
51+
func init() {
52+
rootCmd.AddCommand(versionCmd)
53+
54+
// Here you will define your flags and configuration settings.
55+
56+
// Cobra supports Persistent Flags which will work for this command
57+
// and all subcommands, e.g.:
58+
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")
59+
60+
// Cobra supports local flags which will only run when this command
61+
// is called directly, e.g.:
62+
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
63+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.16
55
require (
66
github.com/aws/aws-sdk-go-v2 v1.3.2
77
github.com/mitchellh/go-homedir v1.1.0
8+
github.com/sirupsen/logrus v1.2.0
89
github.com/spf13/cobra v1.1.3
910
github.com/spf13/viper v1.7.1
1011
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV
109109
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
110110
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
111111
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
112+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
112113
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
113114
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
114115
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
@@ -159,6 +160,7 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
159160
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
160161
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
161162
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
163+
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
162164
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
163165
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
164166
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
@@ -199,6 +201,7 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
199201
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
200202
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
201203
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
204+
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 h1:58fnuSXlxZmFdJyvtTFVmVhcMLU6v5fEb/ok4wyqtNU=
202205
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
203206
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
204207
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=

pkg/awselbv2/awselbv2_test.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

pkg/awselbv2/awselbv2.go renamed to pkg/myaws/myaws.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
package awselbv2
1+
package myaws
22

33
import (
4+
"net/url"
5+
"regexp"
46
"strings"
57

68
"github.com/aws/aws-sdk-go-v2/aws/arn"
9+
log "github.com/sirupsen/logrus"
710
)
811

912
func ListeneRule(arnstr string) string {
@@ -28,7 +31,28 @@ func ListeneRule(arnstr string) string {
2831
url += ";accountId="
2932
url += parn.AccountID
3033

31-
//fmt.Println(url)
34+
// fmt.Println(url)
3235
return url
36+
}
37+
38+
func OpenS3(s3path string) string {
39+
r := regexp.MustCompile(`/`)
40+
str := r.Split(s3path, 2)
41+
log.WithFields(
42+
log.Fields{
43+
"s3 args": str,
44+
}).Debug()
45+
u, _ := url.Parse("https://s3.console.aws.amazon.com/s3/")
46+
q := u.Query()
47+
u.Path = "s3/"
48+
if len(str) > 1 {
49+
q.Set("prefix", str[1])
50+
u.Path += "object/" + str[0]
51+
} else {
52+
q.Set("tab", "objects")
53+
u.Path += "buckets/" + str[0]
54+
}
3355

56+
u.RawQuery = q.Encode()
57+
return u.String()
3458
}

pkg/myaws/myaws_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package myaws
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestListeneRule(t *testing.T) {
8+
// https://docs.aws.amazon.com/ja_jp/general/latest/gr/aws-arns-and-namespaces.html
9+
str := "arn:aws:elasticloadbalancing:us-west-2:123456789012:listener-rule/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2/9683b2d02a6cabee"
10+
11+
want := "https://us-west-2.console.aws.amazon.com/ec2/v2/home?region=us-west-2#ELBRules:type=app;loadBalancerName=my-load-balancer;loadBalancerId=50dc6c495c0c9188;listenerId=f2f7dc8efc522ab2;accountId=123456789012"
12+
got := ListeneRule(str)
13+
14+
if got != want {
15+
t.Errorf("ListenerRule() \ngot=\n%v \nwant=\n%v", got, want)
16+
}
17+
}
18+
19+
func TestOpenS3(t *testing.T) {
20+
a := []struct {
21+
input string
22+
want string
23+
}{
24+
{
25+
input: "cf-templates-xxxxxxxxxxx-ap-northeast-1",
26+
want: "https://s3.console.aws.amazon.com/s3/buckets/cf-templates-xxxxxxxxxxx-ap-northeast-1?tab=objects",
27+
},
28+
{
29+
input: "cf-templates-xxxxxxxxx-ap-northeast-1/20130701Ac-cf2.txt",
30+
want: "https://s3.console.aws.amazon.com/s3/object/cf-templates-xxxxxxxxx-ap-northeast-1?prefix=20130701Ac-cf2.txt",
31+
},
32+
}
33+
34+
for _, v := range a {
35+
got := OpenS3(v.input)
36+
37+
if got != v.want {
38+
t.Errorf("OpenS3() \ninput=%v\n got=%v\n want=%v\n", v.input, got, v.want)
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)