From dbe93ae65b5255ca442dc041ff2495d960fcc7cb Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Sun, 16 Jan 2022 22:36:19 +0100 Subject: [PATCH 01/24] feat: sammy cli --- .dockerignore | 5 +++ .gitignore | 18 ++-------- Dockerfile | 26 ++++++++++++++ Makefile | 11 ++++++ go.mod | 5 +++ go.sum | 84 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 228 insertions(+), 15 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..811c1ad --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +Dockerfile +Makefile +.idea +.gitignore + diff --git a/.gitignore b/.gitignore index 66fd13c..8e8b2ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,3 @@ -# Binaries for programs and plugins -*.exe -*.exe~ -*.dll -*.so -*.dylib - -# Test binary, built with `go test -c` -*.test - -# Output of the go coverage tool, specifically when used with LiteIDE -*.out - -# Dependency directories (remove the comment below to include it) -# vendor/ +sammy +deployment/ +.idea/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3c79abc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +# ---- +# bui# ld executable binary +FROM golang:latest as builder + +WORKDIR $GOPATH/src/github.com/osscameroon/ +# We only copy our app +COPY . . + + +WORKDIR $GOPATH/src/github.com/osscameroon/ +# We fetch dependencies +RUN go get -d -v + +# We compile +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /go/bin/sammy + +# --- +# Let's build our small image +FROM scratch + +# Copy our static executable. +COPY --from=builder /go/bin/sammy /go/bin/sammy + +# Run the hello binary. +ENTRYPOINT ["/go/bin/sammy"] + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0e6cdc6 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +docker-build: + docker build --no-cache -t osscameroon/sammy:latest -f Dockerfile . + +run: + go run main.go $(ARGS) + +build: + go build -o sammy + +exec: build + ./sammy $(ARGS) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..30564b5 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/osscameroon/sammy + +go 1.16 + +require github.com/go-git/go-git/v5 v5.4.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2ea7b18 --- /dev/null +++ b/go.sum @@ -0,0 +1,84 @@ +github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= +github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk= +github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0= +github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ= +github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= +github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk= +github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= +github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= +github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= +github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4= +github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E= +github.com/go-git/go-billy/v5 v5.2.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= +github.com/go-git/go-billy/v5 v5.3.1 h1:CPiOUAzKtMRvolEKw+bG1PLRpT7D3LIs3/3ey4Aiu34= +github.com/go-git/go-billy/v5 v5.3.1/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= +github.com/go-git/go-git-fixtures/v4 v4.2.1/go.mod h1:K8zd3kDUAykwTdDCr+I0per6Y6vMiRR/nnVTBtavnB0= +github.com/go-git/go-git/v5 v5.4.2 h1:BXyZu9t0VkbiHtqrsvdq39UDhGJTl1h55VW6CSC4aY4= +github.com/go-git/go-git/v5 v5.4.2/go.mod h1:gQ1kArt6d+n+BGd+/B/I74HwRTLhth2+zti4ihgckDc= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= +github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= +github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck= +github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI= +github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0= +golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210326060303-6b1517762897 h1:KrsHThm5nFk34YtATK1LsThyGhGbGe1olrte/HInHvs= +golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 h1:RX8C8PRZc2hTIod4ds8ij+/4RQX3AqhYj3uOHmyaz4E= +golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= +gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..24b9d87 --- /dev/null +++ b/main.go @@ -0,0 +1,94 @@ +package main + +import ( + "os" + "path/filepath" + + "github.com/go-git/go-git/v5" +) + +func main() { + if len(os.Args) == 0 { + println("[x] No command provided") + return + } + + switch command := os.Args[1]; command { + case "deploy": + println("[-] sammy deploy... ") + dir := os.Args[2] + deploy(dir) + case "restart-nginx": + println("[-] sammy restart-nginx command...") + case "stop": + println("[-] sammy stop command...") + case "update-field": + println("[-] sammy update-field command...") + default: + println("[x] No valid command provided...") + helperFunc() + } + +} + +func deploy(dir string) { + + println("[>] DEPLOY_DIR : " + dir) + if _, err := os.Stat(dir); err != nil { + if os.IsNotExist(err) { + println("[x] ERR: " + dir + " doesn't exist.") + println(err) + return + } else { + println("[x] ERR: Unexpected error occured for the dir: " + dir) + println(err) + return + } + } + servicesDir := dir + "/services" + confDir := dir + "/conf" + + println("[>] CONF_DIR " + confDir) + listContent(confDir) + + println("[>] SERVICE_DIR " + servicesDir) + listContent(servicesDir) + + // We instantiate a new repository targeting the given path (the .git folder) + r, err := git.PlainOpen(dir) + if err != nil { + println(err) + } + // Get the working directory for the repository + w, err := r.Worktree() + if err != nil { + println(err) + } + + // Pull the latest changes from the origin remote and merge into the current branch + println("git pull origin") + err = w.Pull(&git.PullOptions{RemoteName: "origin"}) + if err != nil { + println(err) + } +} + +func listContent(stringPath string) { + err2 := filepath.Walk(stringPath, + func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if path != stringPath { + println(" " + path) + } + return nil + }) + if err2 != nil { + println(err2) + } +} + +func helperFunc() { + println("\nEx: use sammy...") +} From 10b85de771c3ba7f2d32bf5a3e5380e3b0982383 Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Sun, 16 Jan 2022 22:41:06 +0100 Subject: [PATCH 02/24] feat: add stuffs on gitignore --- .gitignore | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.gitignore b/.gitignore index 8e8b2ee..996ad26 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,19 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + sammy deployment/ .idea/ From 68455b509bda10b15d914776e82fe4b2b22a7970 Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Wed, 19 Jan 2022 01:14:29 +0100 Subject: [PATCH 03/24] feat: deploy command --- go.sum | 17 +++++++++++++++++ main.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/go.sum b/go.sum index 2ea7b18..6fd93b0 100644 --- a/go.sum +++ b/go.sum @@ -5,23 +5,29 @@ github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C6 github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk= github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= +github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0= github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4= github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E= github.com/go-git/go-billy/v5 v5.2.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= github.com/go-git/go-billy/v5 v5.3.1 h1:CPiOUAzKtMRvolEKw+bG1PLRpT7D3LIs3/3ey4Aiu34= github.com/go-git/go-billy/v5 v5.3.1/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= +github.com/go-git/go-git-fixtures/v4 v4.2.1 h1:n9gGL1Ct/yIw+nfsfr8s4+sbhT+Ncu2SubfXjIWgci8= github.com/go-git/go-git-fixtures/v4 v4.2.1/go.mod h1:K8zd3kDUAykwTdDCr+I0per6Y6vMiRR/nnVTBtavnB0= github.com/go-git/go-git/v5 v5.4.2 h1:BXyZu9t0VkbiHtqrsvdq39UDhGJTl1h55VW6CSC4aY4= github.com/go-git/go-git/v5 v5.4.2/go.mod h1:gQ1kArt6d+n+BGd+/B/I74HwRTLhth2+zti4ihgckDc= +github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= @@ -32,16 +38,21 @@ github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgy github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A= github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= @@ -50,6 +61,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI= github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0= @@ -69,16 +81,21 @@ golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 h1:RX8C8PRZc2hTIod4ds8ij+/4RQX3AqhYj3uOHmyaz4E= golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 24b9d87..03482ec 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,9 @@ package main import ( + "fmt" "os" + "os/exec" "path/filepath" "github.com/go-git/go-git/v5" @@ -15,7 +17,7 @@ func main() { switch command := os.Args[1]; command { case "deploy": - println("[-] sammy deploy... ") + println("[-] sammy deploy command... ") dir := os.Args[2] deploy(dir) case "restart-nginx": @@ -32,7 +34,6 @@ func main() { } func deploy(dir string) { - println("[>] DEPLOY_DIR : " + dir) if _, err := os.Stat(dir); err != nil { if os.IsNotExist(err) { @@ -45,6 +46,23 @@ func deploy(dir string) { return } } + + listDeployFolders(dir) + + gitPull(dir) + + // We run the docker-compose here + for _, p := range getDockerComposeFiles(dir) { + exec.Command("docker-compose", "-f "+p+" up -d") + } + + // we run the docker stack deploy here + for _, p := range getDockerStackFiles(dir) { + exec.Command("docker stack deploy", "-c "+p) + } +} + +func listDeployFolders(dir string) { servicesDir := dir + "/services" confDir := dir + "/conf" @@ -53,9 +71,37 @@ func deploy(dir string) { println("[>] SERVICE_DIR " + servicesDir) listContent(servicesDir) +} + +func getDockerComposeFiles(dir string) []string { + return findFile(dir, []string{"*-compose.yml", "*-compose.yaml"}) +} + +func getDockerStackFiles(dir string) []string { + return findFile(dir, []string{"*-stack.yml", "*-stack.yaml"}) +} + +func findFile(targetDir string, pattern []string) []string { + result := []string{} + + for _, v := range pattern { + matches, err := filepath.Glob(targetDir + v) + + if err != nil { + fmt.Println(err) + } + + if len(matches) != 0 { + fmt.Println("Found : ", matches) + result = append(result, matches...) + } + } + return result +} +func gitPull(targetDir string) { // We instantiate a new repository targeting the given path (the .git folder) - r, err := git.PlainOpen(dir) + r, err := git.PlainOpen(targetDir) if err != nil { println(err) } From e94d4639fc5b14e3a920cd3236b53b2633975d06 Mon Sep 17 00:00:00 2001 From: darker Date: Thu, 20 Jan 2022 11:32:42 +0100 Subject: [PATCH 04/24] Update Dockerfile Co-authored-by: Boris Mbarga --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 3c79abc..04d0252 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # ---- -# bui# ld executable binary +# build executable binary FROM golang:latest as builder WORKDIR $GOPATH/src/github.com/osscameroon/ From aa0255a85c8bb1845ce1802ae9f6ee01f0bad4e7 Mon Sep 17 00:00:00 2001 From: darker Date: Thu, 20 Jan 2022 11:32:56 +0100 Subject: [PATCH 05/24] Update main.go Co-authored-by: Boris Mbarga --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 03482ec..5583d31 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,7 @@ func main() { } func deploy(dir string) { - println("[>] DEPLOY_DIR : " + dir) + fmt.Printf("Deploying %s\n", dir) if _, err := os.Stat(dir); err != nil { if os.IsNotExist(err) { println("[x] ERR: " + dir + " doesn't exist.") From 0d5f79286cf310d239a64de45184e325cb6d7e93 Mon Sep 17 00:00:00 2001 From: darker Date: Thu, 20 Jan 2022 11:33:02 +0100 Subject: [PATCH 06/24] Update main.go Co-authored-by: Boris Mbarga --- main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.go b/main.go index 5583d31..ff1638f 100644 --- a/main.go +++ b/main.go @@ -41,8 +41,7 @@ func deploy(dir string) { println(err) return } else { - println("[x] ERR: Unexpected error occured for the dir: " + dir) - println(err) + fmt.Printf("Error: unexpected error occurred for %s: %s\n", dir, err) return } } From 5b5dbf05832da4bc64c46543a3224fffba63d676 Mon Sep 17 00:00:00 2001 From: darker Date: Thu, 20 Jan 2022 11:33:16 +0100 Subject: [PATCH 07/24] Update main.go Co-authored-by: Boris Mbarga --- main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.go b/main.go index ff1638f..f09908e 100644 --- a/main.go +++ b/main.go @@ -37,8 +37,7 @@ func deploy(dir string) { fmt.Printf("Deploying %s\n", dir) if _, err := os.Stat(dir); err != nil { if os.IsNotExist(err) { - println("[x] ERR: " + dir + " doesn't exist.") - println(err) + fmt.Printf("Error: %s: %s\n", dir, err) return } else { fmt.Printf("Error: unexpected error occurred for %s: %s\n", dir, err) From 8e0f1c8abcb11afae21025d50e04f98a05b7c5b5 Mon Sep 17 00:00:00 2001 From: darker Date: Thu, 20 Jan 2022 11:33:27 +0100 Subject: [PATCH 08/24] Update main.go Co-authored-by: Boris Mbarga --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index f09908e..db1d5fd 100644 --- a/main.go +++ b/main.go @@ -64,7 +64,7 @@ func listDeployFolders(dir string) { servicesDir := dir + "/services" confDir := dir + "/conf" - println("[>] CONF_DIR " + confDir) + fmt.Printf("Config folder %s\n", confDir) listContent(confDir) println("[>] SERVICE_DIR " + servicesDir) From 9c063cbf3437c5530b06a476189c903781f721e6 Mon Sep 17 00:00:00 2001 From: darker Date: Thu, 20 Jan 2022 11:33:37 +0100 Subject: [PATCH 09/24] Update main.go Co-authored-by: Boris Mbarga --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index db1d5fd..2920b10 100644 --- a/main.go +++ b/main.go @@ -67,7 +67,7 @@ func listDeployFolders(dir string) { fmt.Printf("Config folder %s\n", confDir) listContent(confDir) - println("[>] SERVICE_DIR " + servicesDir) + fmt.Println("Services folder %s\n", servicesDir) listContent(servicesDir) } From 486d8a8d8df5d50fd26e072150ac0eeb025a4038 Mon Sep 17 00:00:00 2001 From: darker Date: Thu, 20 Jan 2022 11:34:11 +0100 Subject: [PATCH 10/24] Update main.go Co-authored-by: Boris Mbarga --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 2920b10..d3c706b 100644 --- a/main.go +++ b/main.go @@ -118,7 +118,7 @@ func gitPull(targetDir string) { } func listContent(stringPath string) { - err2 := filepath.Walk(stringPath, + err := filepath.Walk(stringPath, func(path string, info os.FileInfo, err error) error { if err != nil { return err From 8741ab44b44f0b3c031d689c928537c2102e7cdd Mon Sep 17 00:00:00 2001 From: darker Date: Thu, 20 Jan 2022 11:34:20 +0100 Subject: [PATCH 11/24] Update main.go Co-authored-by: Boris Mbarga --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index d3c706b..d54b32b 100644 --- a/main.go +++ b/main.go @@ -128,8 +128,8 @@ func listContent(stringPath string) { } return nil }) - if err2 != nil { - println(err2) + if err != nil { + fmt.Printf("Error: failed to list content: %s\n", err) } } From 98fdd84223ab36127a37addef96edd650ddfc583 Mon Sep 17 00:00:00 2001 From: darker Date: Thu, 20 Jan 2022 11:34:34 +0100 Subject: [PATCH 12/24] Update main.go Co-authored-by: Boris Mbarga --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index d54b32b..51f912c 100644 --- a/main.go +++ b/main.go @@ -84,7 +84,6 @@ func findFile(targetDir string, pattern []string) []string { for _, v := range pattern { matches, err := filepath.Glob(targetDir + v) - if err != nil { fmt.Println(err) } From c727044a058c2e620a4b99310811005367b0307e Mon Sep 17 00:00:00 2001 From: darker Date: Thu, 20 Jan 2022 11:34:41 +0100 Subject: [PATCH 13/24] Update main.go Co-authored-by: Boris Mbarga --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 51f912c..1e27295 100644 --- a/main.go +++ b/main.go @@ -79,7 +79,7 @@ func getDockerStackFiles(dir string) []string { return findFile(dir, []string{"*-stack.yml", "*-stack.yaml"}) } -func findFile(targetDir string, pattern []string) []string { +func findFiles(targetDir string, pattern []string) []string { result := []string{} for _, v := range pattern { From 59f4dbab802c722469c7e31405fb41f4bfb3c8a4 Mon Sep 17 00:00:00 2001 From: darker Date: Thu, 20 Jan 2022 11:34:56 +0100 Subject: [PATCH 14/24] Update main.go Co-authored-by: Boris Mbarga --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 1e27295..4f99abe 100644 --- a/main.go +++ b/main.go @@ -89,7 +89,7 @@ func findFiles(targetDir string, pattern []string) []string { } if len(matches) != 0 { - fmt.Println("Found : ", matches) + fmt.Println("Found: ", matches) result = append(result, matches...) } } From 8096c9b29c382d2dcaae1f724adebca109fff973 Mon Sep 17 00:00:00 2001 From: darker Date: Thu, 20 Jan 2022 11:35:03 +0100 Subject: [PATCH 15/24] Update main.go Co-authored-by: Boris Mbarga --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 4f99abe..54f0e90 100644 --- a/main.go +++ b/main.go @@ -85,7 +85,7 @@ func findFiles(targetDir string, pattern []string) []string { for _, v := range pattern { matches, err := filepath.Glob(targetDir + v) if err != nil { - fmt.Println(err) + fmt.Printf("Error: failed to find files: %s\n"err) } if len(matches) != 0 { From 662cf3aa247132df84b4b8e485ad8ef10a422365 Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Wed, 2 Feb 2022 21:30:00 +0100 Subject: [PATCH 16/24] fix: fix fmt print and findFile typo --- main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 54f0e90..1a785dc 100644 --- a/main.go +++ b/main.go @@ -67,16 +67,16 @@ func listDeployFolders(dir string) { fmt.Printf("Config folder %s\n", confDir) listContent(confDir) - fmt.Println("Services folder %s\n", servicesDir) + fmt.Printf("Services folder %s\n", servicesDir) listContent(servicesDir) } func getDockerComposeFiles(dir string) []string { - return findFile(dir, []string{"*-compose.yml", "*-compose.yaml"}) + return findFiles(dir, []string{"*-compose.yml", "*-compose.yaml"}) } func getDockerStackFiles(dir string) []string { - return findFile(dir, []string{"*-stack.yml", "*-stack.yaml"}) + return findFiles(dir, []string{"*-stack.yml", "*-stack.yaml"}) } func findFiles(targetDir string, pattern []string) []string { @@ -85,7 +85,7 @@ func findFiles(targetDir string, pattern []string) []string { for _, v := range pattern { matches, err := filepath.Glob(targetDir + v) if err != nil { - fmt.Printf("Error: failed to find files: %s\n"err) + fmt.Printf("Error: failed to find files: %s\n", err) } if len(matches) != 0 { From ac22d99d2ad1135a625254c7485903f4aa325e6b Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Wed, 2 Feb 2022 21:30:32 +0100 Subject: [PATCH 17/24] feat: updated switch cases - added a throError method - updated choices input in the switch case --- main.go | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 1a785dc..78e5ed9 100644 --- a/main.go +++ b/main.go @@ -10,30 +10,43 @@ import ( ) func main() { - if len(os.Args) == 0 { - println("[x] No command provided") - return - } - switch command := os.Args[1]; command { - case "deploy": - println("[-] sammy deploy command... ") - dir := os.Args[2] - deploy(dir) - case "restart-nginx": - println("[-] sammy restart-nginx command...") - case "stop": - println("[-] sammy stop command...") - case "update-field": - println("[-] sammy update-field command...") - default: - println("[x] No valid command provided...") - helperFunc() + command := os.Args + + if len(command) > 1 { + switch command[1] { + case "help": + helperFunc() + case "h": + helperFunc() + case "deploy": + if len(command) > 2 { + deploy(command[2]) + } else { + throwError("No Directory provided !", 1) + } + case "restart-nginx": + fmt.Println("[-] sammy restart-nginx command...") + case "stop": + fmt.Println("[-] sammy stop command...") + case "update-field": + fmt.Println("[-] sammy update-field command...") + default: + throwError("No valid command provided...", 0) + } + } else { + throwError("No command provided", 0) } +} +func throwError(err string, statusCode int) { + fmt.Printf("[x] %s", err) + helperFunc() + os.Exit(statusCode) } func deploy(dir string) { + fmt.Println("[-] sammy deploy command... ") fmt.Printf("Deploying %s\n", dir) if _, err := os.Stat(dir); err != nil { if os.IsNotExist(err) { From f7ebca837c995d73f923db389cccf042858ac367 Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Wed, 2 Feb 2022 21:41:32 +0100 Subject: [PATCH 18/24] feat: clean throw error function --- main.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 78e5ed9..b01598f 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ func main() { deploy(command[2]) } else { throwError("No Directory provided !", 1) + helperFunc() } case "restart-nginx": fmt.Println("[-] sammy restart-nginx command...") @@ -33,15 +34,16 @@ func main() { fmt.Println("[-] sammy update-field command...") default: throwError("No valid command provided...", 0) + helperFunc() } } else { throwError("No command provided", 0) + helperFunc() } } func throwError(err string, statusCode int) { - fmt.Printf("[x] %s", err) - helperFunc() + fmt.Printf("[x] Error: %s", err) os.Exit(statusCode) } @@ -50,11 +52,9 @@ func deploy(dir string) { fmt.Printf("Deploying %s\n", dir) if _, err := os.Stat(dir); err != nil { if os.IsNotExist(err) { - fmt.Printf("Error: %s: %s\n", dir, err) - return + throwError("Directory '"+dir+"' not found : "+err.Error(), 1) } else { - fmt.Printf("Error: unexpected error occurred for %s: %s\n", dir, err) - return + throwError(dir+" : "+err.Error(), 1) } } @@ -98,7 +98,7 @@ func findFiles(targetDir string, pattern []string) []string { for _, v := range pattern { matches, err := filepath.Glob(targetDir + v) if err != nil { - fmt.Printf("Error: failed to find files: %s\n", err) + throwError("failed to find files: "+err.Error(), 1) } if len(matches) != 0 { @@ -141,7 +141,7 @@ func listContent(stringPath string) { return nil }) if err != nil { - fmt.Printf("Error: failed to list content: %s\n", err) + throwError("failed to list content: "+err.Error(), 1) } } From dab0b3b27b0b3f310f292ef1cf21223299a9fea1 Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Wed, 2 Feb 2022 21:43:21 +0100 Subject: [PATCH 19/24] feat: clean printing --- main.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index b01598f..6db4714 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,7 @@ func main() { func throwError(err string, statusCode int) { fmt.Printf("[x] Error: %s", err) + fmt.Println() os.Exit(statusCode) } @@ -113,19 +114,19 @@ func gitPull(targetDir string) { // We instantiate a new repository targeting the given path (the .git folder) r, err := git.PlainOpen(targetDir) if err != nil { - println(err) + fmt.Println(err) } // Get the working directory for the repository w, err := r.Worktree() if err != nil { - println(err) + fmt.Println(err) } // Pull the latest changes from the origin remote and merge into the current branch - println("git pull origin") + fmt.Println("git pull origin") err = w.Pull(&git.PullOptions{RemoteName: "origin"}) if err != nil { - println(err) + fmt.Println(err) } } @@ -136,7 +137,7 @@ func listContent(stringPath string) { return err } if path != stringPath { - println(" " + path) + fmt.Println(" " + path) } return nil }) @@ -146,5 +147,5 @@ func listContent(stringPath string) { } func helperFunc() { - println("\nEx: use sammy...") + fmt.Println("\nEx: use sammy...") } From fff568c98de89d9c2ccd8c9cd557cf8d74a7e77d Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Wed, 2 Feb 2022 21:53:54 +0100 Subject: [PATCH 20/24] feat: added logger to throwError --- main.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 6db4714..ecced31 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "log" "os" "os/exec" "path/filepath" @@ -21,6 +22,7 @@ func main() { helperFunc() case "deploy": if len(command) > 2 { + fmt.Println("[-] sammy deploy command... ") deploy(command[2]) } else { throwError("No Directory provided !", 1) @@ -45,11 +47,16 @@ func main() { func throwError(err string, statusCode int) { fmt.Printf("[x] Error: %s", err) fmt.Println() + + if statusCode == 1 { + log.Fatal(err) + } else if statusCode == 0 { + log.Println(err) + } os.Exit(statusCode) } func deploy(dir string) { - fmt.Println("[-] sammy deploy command... ") fmt.Printf("Deploying %s\n", dir) if _, err := os.Stat(dir); err != nil { if os.IsNotExist(err) { From fb739245bb96a606f04c402621d6cb001edcf6b7 Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Wed, 2 Feb 2022 21:56:50 +0100 Subject: [PATCH 21/24] feat: update/fix exec command --- main.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index ecced31..ff2dea7 100644 --- a/main.go +++ b/main.go @@ -56,6 +56,16 @@ func throwError(err string, statusCode int) { os.Exit(statusCode) } +func execCommand(program string, args string) { + cmd := exec.Command(program, args) + + err := cmd.Run() + + if err != nil { + log.Println(err) + } +} + func deploy(dir string) { fmt.Printf("Deploying %s\n", dir) if _, err := os.Stat(dir); err != nil { @@ -72,12 +82,12 @@ func deploy(dir string) { // We run the docker-compose here for _, p := range getDockerComposeFiles(dir) { - exec.Command("docker-compose", "-f "+p+" up -d") + execCommand("docker-compose", "-f "+p+" up -d") } // we run the docker stack deploy here for _, p := range getDockerStackFiles(dir) { - exec.Command("docker stack deploy", "-c "+p) + execCommand("docker stack deploy", "-c "+p) } } From 17344079aea2eac19e0d224d2fb7595cf2cabae5 Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Wed, 2 Feb 2022 22:08:34 +0100 Subject: [PATCH 22/24] feat: added a proceedCommand function to manage the switch --- main.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index ff2dea7..074e856 100644 --- a/main.go +++ b/main.go @@ -11,9 +11,12 @@ import ( ) func main() { - command := os.Args + proceedCommand(command) +} +//proceedCommand will check for given arguments and execute the appropriate operation +func proceedCommand(command []string) { if len(command) > 1 { switch command[1] { case "help": From a3e1001d51b06f98ce4748974423424207acbc17 Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Wed, 2 Feb 2022 22:09:36 +0100 Subject: [PATCH 23/24] fix: updated the throwError func --- main.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 074e856..f88bece 100644 --- a/main.go +++ b/main.go @@ -47,19 +47,19 @@ func proceedCommand(command []string) { } } +//throwError will throw an error message and exit depending on the given status code func throwError(err string, statusCode int) { - fmt.Printf("[x] Error: %s", err) - fmt.Println() - if statusCode == 1 { - log.Fatal(err) + log.Fatal("[x] Error: " + err) } else if statusCode == 0 { - log.Println(err) + log.Println("[x] Error: " + err) } os.Exit(statusCode) } +//execCommand will execute a command with provided program and arguments func execCommand(program string, args string) { + fmt.Println("[-] Exec: " + program + " " + args) cmd := exec.Command(program, args) err := cmd.Run() From b56e6ab85f86fb7bccc2eadb8c619835b6268426 Mon Sep 17 00:00:00 2001 From: sanix-darker Date: Wed, 2 Feb 2022 22:09:53 +0100 Subject: [PATCH 24/24] clean: added comments for each funcs --- main.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/main.go b/main.go index f88bece..656dd2b 100644 --- a/main.go +++ b/main.go @@ -69,6 +69,7 @@ func execCommand(program string, args string) { } } +//deploy is the deployment method that will pull the given repo and execute dockers command func deploy(dir string) { fmt.Printf("Deploying %s\n", dir) if _, err := os.Stat(dir); err != nil { @@ -94,6 +95,7 @@ func deploy(dir string) { } } +//listDeployFolders is an util function to print the content of the config dir and the service dir func listDeployFolders(dir string) { servicesDir := dir + "/services" confDir := dir + "/conf" @@ -105,14 +107,17 @@ func listDeployFolders(dir string) { listContent(servicesDir) } +// getDockerComposeFiles will use findFiles to return all docker-compose files func getDockerComposeFiles(dir string) []string { return findFiles(dir, []string{"*-compose.yml", "*-compose.yaml"}) } +//getDockerStackFiles will use findFiles to return all docker-stack files func getDockerStackFiles(dir string) []string { return findFiles(dir, []string{"*-stack.yml", "*-stack.yaml"}) } +//findFiles is an util function that will search in the tree of a directory for files matching a pattern func findFiles(targetDir string, pattern []string) []string { result := []string{} @@ -130,6 +135,7 @@ func findFiles(targetDir string, pattern []string) []string { return result } +//gitPull will pull recent changes from the current dir func gitPull(targetDir string) { // We instantiate a new repository targeting the given path (the .git folder) r, err := git.PlainOpen(targetDir) @@ -150,6 +156,7 @@ func gitPull(targetDir string) { } } +//listContent will just list the content of a given directory func listContent(stringPath string) { err := filepath.Walk(stringPath, func(path string, info os.FileInfo, err error) error { @@ -166,6 +173,7 @@ func listContent(stringPath string) { } } +//helperFunc is an util function to list available commands func helperFunc() { fmt.Println("\nEx: use sammy...") }