From 5d244e53e1e63e4d72fee855d69c95ec199dc359 Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 19:32:51 +0100 Subject: [PATCH 01/15] Fix deprecation, shadowed variable and identical package name coalition --- cloud.go | 29 +++++++++++++++-------------- cloud_test.go | 5 ++--- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cloud.go b/cloud.go index 817f469..db7ae4b 100644 --- a/cloud.go +++ b/cloud.go @@ -4,14 +4,15 @@ import ( "bytes" "encoding/xml" "fmt" - "io/ioutil" + "io" "net/http" "net/url" + "os" "path/filepath" "strings" ) -// A client represents a client connection to a {own|next}cloud +// Client represents a client connection to a {own|next}cloud type Client struct { Url *url.URL Username string @@ -51,12 +52,12 @@ type ShareResult struct { // Dial connects to an {own|next}Cloud instance at the specified // address using the given credentials. func Dial(host, username, password string) (*Client, error) { - url, err := url.Parse(host) + parsedUrl, err := url.Parse(host) if err != nil { return nil, err } return &Client{ - Url: url, + Url: parsedUrl, Username: username, Password: password, }, nil @@ -90,7 +91,7 @@ func (c *Client) UploadDir(src string, dest string) ([]string, error) { return nil, err } for _, file := range files { - data, err := ioutil.ReadFile(file) + data, err := os.ReadFile(file) if err != nil { return nil, err } @@ -178,20 +179,20 @@ func (c *Client) sendWebDavRequest(request string, path string, data []byte) ([] return nil, err } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } if len(body) > 0 { if body[0] == '<' { - error := Error{} - err = xml.Unmarshal(body, &error) - if err != nil { - return body, err + reqErr := &Error{} + decodeErr := xml.Unmarshal(body, reqErr) + if decodeErr != nil { + return body, decodeErr } - if error.Exception != "" { - return nil, err + if reqErr.Exception != "" { + return nil, reqErr } } @@ -226,7 +227,7 @@ func (c *Client) sendAppsRequest(request string, path string, data string) (*Sha return nil, err } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } @@ -269,7 +270,7 @@ func (c *Client) sendOCSRequest(request string, path string, data string) (*Shar return nil, err } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } diff --git a/cloud_test.go b/cloud_test.go index 9b04494..1028f4d 100644 --- a/cloud_test.go +++ b/cloud_test.go @@ -1,7 +1,7 @@ package cloud import ( - "io/ioutil" + "os" "path/filepath" "testing" @@ -34,7 +34,6 @@ func (t *testSuite) BeforeAll() { if err != nil { panic(err) } - } func (t *testSuite) After() { @@ -57,7 +56,7 @@ func (t *testSuite) TestDownloadUpload() { err := client.Mkdir("Test") t.Nil(err) - src, err := ioutil.ReadFile(filepath.Join(testDir, "test.txt")) + src, err := os.ReadFile(filepath.Join(testDir, "test.txt")) err = client.Upload(src, "Test/test.txt") t.Nil(err) From 4d9949b6563d4d6ebd8e6066cf4b2b2aec442b91 Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 21:03:19 +0100 Subject: [PATCH 02/15] Add idea directory to gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5236e1e..f166652 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ *~ - +.idea From aa22ac5d4a5c27d1a0e636a0cf1d5ea6c190efa0 Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 21:05:11 +0100 Subject: [PATCH 03/15] Move client code to package --- cloud.go => pkg/cloud/cloud.go | 0 cloud_test.go => pkg/cloud/cloud_test.go | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename cloud.go => pkg/cloud/cloud.go (100%) rename cloud_test.go => pkg/cloud/cloud_test.go (100%) diff --git a/cloud.go b/pkg/cloud/cloud.go similarity index 100% rename from cloud.go rename to pkg/cloud/cloud.go diff --git a/cloud_test.go b/pkg/cloud/cloud_test.go similarity index 100% rename from cloud_test.go rename to pkg/cloud/cloud_test.go From 26748ff233efdb1997a485d749727dbdeca45758 Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 21:05:25 +0100 Subject: [PATCH 04/15] Init go module --- go.mod | 11 +++++++++++ go.sum | 9 +++++++++ 2 files changed, 20 insertions(+) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4521d64 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/eu-erwin/nextcloud-cli + +go 1.19 + +require github.com/remogatto/prettytest v0.0.0-20200211072524-6d385e11dcb8 + +require ( + github.com/kr/pretty v0.2.1 // indirect + github.com/kr/text v0.1.0 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..84250d7 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +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 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/remogatto/prettytest v0.0.0-20200211072524-6d385e11dcb8 h1:nRDwTcxV9B3elxMt+1xINX0bwaPdpouqp5fbynexY8U= +github.com/remogatto/prettytest v0.0.0-20200211072524-6d385e11dcb8/go.mod h1:jOEnp79oIHy5cvQSHeLcgVJk1GHOOHJHQWps/d1N5Yo= +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= From 988b0c96fbf0000a27655e0a98df85708b1c4d34 Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 21:30:41 +0100 Subject: [PATCH 05/15] Rename package and clean up warnings --- pkg/{cloud => nextcloud}/cloud.go | 30 +++++++++++++++++++++++--- pkg/{cloud => nextcloud}/cloud_test.go | 28 ++++++++++++++++++------ 2 files changed, 48 insertions(+), 10 deletions(-) rename pkg/{cloud => nextcloud}/cloud.go (92%) rename pkg/{cloud => nextcloud}/cloud_test.go (85%) diff --git a/pkg/cloud/cloud.go b/pkg/nextcloud/cloud.go similarity index 92% rename from pkg/cloud/cloud.go rename to pkg/nextcloud/cloud.go index db7ae4b..f829178 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/nextcloud/cloud.go @@ -1,8 +1,9 @@ -package cloud +package nextcloud import ( "bytes" "encoding/xml" + "errors" "fmt" "io" "net/http" @@ -12,6 +13,29 @@ import ( "strings" ) +func NewStorage( + cloudUrl, + username, + password string, +) (*Client, error) { + if "" == cloudUrl { + return nil, errors.New("missing url") + } + if "" == username || "" == password { + return nil, errors.New("missing credentials") + } + + parsedUrl, err := url.Parse(cloudUrl) + if nil != err { + return nil, errors.New("invalid url") + } + return &Client{ + Url: parsedUrl, + Username: username, + Password: password, + }, nil +} + // Client represents a client connection to a {own|next}cloud type Client struct { Url *url.URL @@ -238,7 +262,7 @@ func (c *Client) sendAppsRequest(request string, path string, data string) (*Sha return nil, err } if result.StatusCode != 100 { - return nil, fmt.Errorf("Share API returned an unsuccessful status code %d", result.StatusCode) + return nil, fmt.Errorf("share API returned an unsuccessful status code %d", result.StatusCode) } return &result, nil @@ -282,7 +306,7 @@ func (c *Client) sendOCSRequest(request string, path string, data string) (*Shar return nil, err } if result.StatusCode != 200 { - return nil, fmt.Errorf("Share API returned an unsuccessful status code %d", result.StatusCode) + return nil, fmt.Errorf("share API returned an unsuccessful status code %d", result.StatusCode) } return &result, nil diff --git a/pkg/cloud/cloud_test.go b/pkg/nextcloud/cloud_test.go similarity index 85% rename from pkg/cloud/cloud_test.go rename to pkg/nextcloud/cloud_test.go index 1028f4d..7d2159c 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/nextcloud/cloud_test.go @@ -1,4 +1,4 @@ -package cloud +package nextcloud import ( "os" @@ -37,7 +37,10 @@ func (t *testSuite) BeforeAll() { } func (t *testSuite) After() { - client.Delete("Test") + err := client.Delete("Test") + if err != nil { + panic("can't delete Test") + } } func (t *testSuite) TestMkDir() { @@ -123,8 +126,10 @@ func (t *testSuite) TestCreateFileDropShare() { t.True(len(result.Url) > 0) } - client.Delete("ShareTest") - + err = client.Delete("ShareTest") + if err != nil { + panic("can't delete ShareTest on TestCreateFileDropShare") + } } func (t *testSuite) TestGetShare() { @@ -138,7 +143,10 @@ func (t *testSuite) TestGetShare() { t.Nil(err) t.True(len(result.Elements) > 0) - client.Delete("ShareTest") + err = client.Delete("ShareTest") + if err != nil { + panic("can't delete ShareTest on TestGetShare") + } } func (t *testSuite) TestDeleteShare() { @@ -159,7 +167,10 @@ func (t *testSuite) TestDeleteShare() { t.Nil(err) t.True(len(result.Elements) == 0) - client.Delete("ShareTest") + err = client.Delete("ShareTest") + if err != nil { + panic("can't delete ShareTest on TestDeleteShare") + } } func (t *testSuite) TestCreateReadOnlyShare() { @@ -180,5 +191,8 @@ func (t *testSuite) TestCreateReadOnlyShare() { t.Nil(err) t.True(len(result.Elements) == 0) - client.Delete("ShareTest") + err = client.Delete("ShareTest") + if err != nil { + panic("can't delete ShareTest on TestCreateReadOnlyShare") + } } From d416e3293d7bf1f436a2db998c858d7ae958833a Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 21:31:31 +0100 Subject: [PATCH 06/15] Create abstraction --- pkg/cloud/storage.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 pkg/cloud/storage.go diff --git a/pkg/cloud/storage.go b/pkg/cloud/storage.go new file mode 100644 index 0000000..f3084f2 --- /dev/null +++ b/pkg/cloud/storage.go @@ -0,0 +1,35 @@ +package cloud + +import "encoding/xml" + +type ShareElement struct { + Id uint `xml:"id"` + Url string `xml:"url"` +} + +type ShareResult struct { + XMLName xml.Name `xml:"ocs"` + Status string `xml:"meta>status"` + StatusCode uint `xml:"meta>statuscode"` + Message string `xml:"meta>message"` + Id uint `xml:"data>id"` + Url string `xml:"data>url"` + Elements []ShareElement `xml:"data>element"` +} + +type Storage interface { + Mkdir(path string) error + Delete(path string) error + Upload(src []byte, dest string) error + UploadDir(src string, dest string) ([]string, error) + Download(path string) ([]byte, error) + Exists(path string) bool + CreateGroupFolder(mountPoint string) (*ShareResult, error) + AddGroupToGroupFolder(group string, folderId uint) (*ShareResult, error) + SetGroupPermissionsForGroupFolder(permissions int, group string, folderId uint) (*ShareResult, error) + CreateShare(path string, shareType int, publicUpload string, permissions int) (*ShareResult, error) + GetShare(path string) (*ShareResult, error) + DeleteShare(id uint) (*ShareResult, error) + CreateFileDropShare(path string) (*ShareResult, error) + CreateReadOnlyShare(path string) (*ShareResult, error) +} From 80d9ba30eee9cd5de91db44a09013598b8a3a565 Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 21:32:40 +0100 Subject: [PATCH 07/15] Implement storage interfaces --- pkg/nextcloud/cloud.go | 47 ++++++++++++++----------------------- pkg/nextcloud/cloud_test.go | 5 +--- 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/pkg/nextcloud/cloud.go b/pkg/nextcloud/cloud.go index f829178..69863ae 100644 --- a/pkg/nextcloud/cloud.go +++ b/pkg/nextcloud/cloud.go @@ -11,13 +11,15 @@ import ( "os" "path/filepath" "strings" + + "github.com/eu-erwin/nextcloud-cli/pkg/cloud" ) func NewStorage( cloudUrl, username, password string, -) (*Client, error) { +) (cloud.Storage, error) { if "" == cloudUrl { return nil, errors.New("missing url") } @@ -58,21 +60,6 @@ func (e *Error) Error() string { return fmt.Sprintf("Exception: %s, Message: %s", e.Exception, e.Message) } -type ShareElement struct { - Id uint `xml:"id"` - Url string `xml:"url"` -} - -type ShareResult struct { - XMLName xml.Name `xml:"ocs"` - Status string `xml:"meta>status"` - StatusCode uint `xml:"meta>statuscode"` - Message string `xml:"meta>message"` - Id uint `xml:"data>id"` - Url string `xml:"data>url"` - Elements []ShareElement `xml:"data>element"` -} - // Dial connects to an {own|next}Cloud instance at the specified // address using the given credentials. func Dial(host, username, password string) (*Client, error) { @@ -138,31 +125,31 @@ func (c *Client) Exists(path string) bool { return err == nil } -func (c *Client) CreateGroupFolder(mountPoint string) (*ShareResult, error) { +func (c *Client) CreateGroupFolder(mountPoint string) (*cloud.ShareResult, error) { return c.sendAppsRequest("POST", "groupfolders/folders", fmt.Sprintf("mountpoint=%s", mountPoint)) } -func (c *Client) AddGroupToGroupFolder(group string, folderId uint) (*ShareResult, error) { +func (c *Client) AddGroupToGroupFolder(group string, folderId uint) (*cloud.ShareResult, error) { return c.sendAppsRequest("POST", fmt.Sprintf("groupfolders/folders/%d/groups", folderId), fmt.Sprintf("group=%s", group)) } -func (c *Client) SetGroupPermissionsForGroupFolder(permissions int, group string, folderId uint) (*ShareResult, error) { +func (c *Client) SetGroupPermissionsForGroupFolder(permissions int, group string, folderId uint) (*cloud.ShareResult, error) { return c.sendAppsRequest("POST", fmt.Sprintf("apps/groupfolders/folders/%d/groups/%s", folderId, group), fmt.Sprintf("permissions=%d", permissions)) } -func (c *Client) CreateShare(path string, shareType int, publicUpload string, permissions int) (*ShareResult, error) { +func (c *Client) CreateShare(path string, shareType int, publicUpload string, permissions int) (*cloud.ShareResult, error) { return c.sendOCSRequest("POST", "shares", fmt.Sprintf("path=%s&shareType=%d&publicUpload=%s&permissions=%d", path, shareType, publicUpload, permissions)) } -func (c *Client) GetShare(path string) (*ShareResult, error) { +func (c *Client) GetShare(path string) (*cloud.ShareResult, error) { return c.sendOCSRequest("GET", fmt.Sprintf("shares?path=%s", path), "") } -func (c *Client) DeleteShare(id uint) (*ShareResult, error) { +func (c *Client) DeleteShare(id uint) (*cloud.ShareResult, error) { return c.sendOCSRequest("DELETE", fmt.Sprintf("shares/%d", id), "") } -func (c *Client) CreateFileDropShare(path string) (*ShareResult, error) { +func (c *Client) CreateFileDropShare(path string) (*cloud.ShareResult, error) { result, err := c.CreateShare(path, 3, "true", 4) if err != nil { return nil, err @@ -171,7 +158,7 @@ func (c *Client) CreateFileDropShare(path string) (*ShareResult, error) { return c.sendOCSRequest("PUT", fmt.Sprintf("shares/%d", id), "permissions=4") } -func (c *Client) CreateReadOnlyShare(path string) (*ShareResult, error) { +func (c *Client) CreateReadOnlyShare(path string) (*cloud.ShareResult, error) { result, err := c.CreateShare(path, 3, "true", 4) if err != nil { return nil, err @@ -225,7 +212,7 @@ func (c *Client) sendWebDavRequest(request string, path string, data []byte) ([] return body, nil } -func (c *Client) sendAppsRequest(request string, path string, data string) (*ShareResult, error) { +func (c *Client) sendAppsRequest(request string, path string, data string) (*cloud.ShareResult, error) { // Create the https request appsPath := filepath.Join("apps", path) @@ -256,7 +243,7 @@ func (c *Client) sendAppsRequest(request string, path string, data string) (*Sha return nil, err } - result := ShareResult{} + result := cloud.ShareResult{} err = xml.Unmarshal(body, &result) if err != nil { return nil, err @@ -268,7 +255,7 @@ func (c *Client) sendAppsRequest(request string, path string, data string) (*Sha return &result, nil } -func (c *Client) sendOCSRequest(request string, path string, data string) (*ShareResult, error) { +func (c *Client) sendOCSRequest(request string, path string, data string) (*cloud.ShareResult, error) { // Create the https request appsPath := filepath.Join("ocs/v2.php/apps/files_sharing/api/v1", path) @@ -299,9 +286,9 @@ func (c *Client) sendOCSRequest(request string, path string, data string) (*Shar return nil, err } - result := ShareResult{} + result := &cloud.ShareResult{} - err = xml.Unmarshal(body, &result) + err = xml.Unmarshal(body, result) if err != nil { return nil, err } @@ -309,5 +296,5 @@ func (c *Client) sendOCSRequest(request string, path string, data string) (*Shar return nil, fmt.Errorf("share API returned an unsuccessful status code %d", result.StatusCode) } - return &result, nil + return result, nil } diff --git a/pkg/nextcloud/cloud_test.go b/pkg/nextcloud/cloud_test.go index 7d2159c..a23ae15 100644 --- a/pkg/nextcloud/cloud_test.go +++ b/pkg/nextcloud/cloud_test.go @@ -37,10 +37,7 @@ func (t *testSuite) BeforeAll() { } func (t *testSuite) After() { - err := client.Delete("Test") - if err != nil { - panic("can't delete Test") - } + _ = client.Delete("Test") } func (t *testSuite) TestMkDir() { From 0b69194770f0a352c9ccb8b94cebac3ce64ed98a Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 21:33:03 +0100 Subject: [PATCH 08/15] Add upload command --- README.md | 17 ++++++++++ cmd/cli.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 7 +++++ 3 files changed, 116 insertions(+) create mode 100644 cmd/cli.go create mode 100644 main.go diff --git a/README.md b/README.md index 5c4ccd8..853d8de 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,23 @@ This is a golang client for [ownCloud](https://owncloud.com) and [NextCloud](https://nextcloud.com). +# Usages +Installation +```shell +go install github.com/eu-erwin/nextcloud-cli +``` + +# Usages +Available cli command +- upload + +## Upload command +To upload a file for example `README.md` from current working directory to directory `Notes` in nextcloud. simply use +```shell +nextcloud-cli --username=hello --password=world --url=http://localhost:18080/ --path=Notes README.md +``` + + # LICENSE MIT diff --git a/cmd/cli.go b/cmd/cli.go new file mode 100644 index 0000000..6c173a5 --- /dev/null +++ b/cmd/cli.go @@ -0,0 +1,92 @@ +package cli + +import ( + "flag" + "fmt" + "log" + "os" + "strings" + + "github.com/eu-erwin/nextcloud-cli/pkg/cloud" + "github.com/eu-erwin/nextcloud-cli/pkg/nextcloud" +) + +var ( + client cloud.Storage + targetPath *string + wd string +) + +func init() { + cloudUrl := flag.String("url", "", "Please enter url") + username := flag.String("username", "", "Please enter username") + password := flag.String("password", "", "Please enter password") + targetPath = flag.String("path", "", "Target path to upload file") + flag.Parse() + + storage, err := nextcloud.NewStorage(*cloudUrl, *username, *password) + if nil != err { + log.Println("storage can't be created. reason: ", err.Error()) + return + } + + client = storage + wd, _ = os.Getwd() + _ = []string{wd} +} + +func printHelp() { + log.Println(`Available command: +upload + +Available flags: +--url url of the nextcloud host (*) +--username your username (*) +--password your password (*) +--path target path + +Ex.: nextcloud-cli upload --username=john --password=supersecret --url=https://cloud.example.com hello.text`) +} + +func Run() { + args := flag.Args() + if 0 == len(args) { + log.Println(`missing command`) + printHelp() + return + } + + switch args[0] { + case "upload": + upload(args[1:]...) + default: + log.Println(`missing command`) + printHelp() + } +} + +func upload(sources ...string) { + for _, source := range sources { + fmt.Printf("Uploading %s\r\n", source) + + content, err := os.ReadFile(strings.Join([]string{wd, source}, "/")) + if nil != err { + log.Println("Can't upload", source, err.Error()) + continue + } + + target := source + if "" != *targetPath { + if err = client.Mkdir(*targetPath); nil == err { + log.Println("New directory created", source) + } + target = strings.Join([]string{*targetPath, source}, "/") + } + + err = client.Upload(content, target) + if nil != err { + log.Println("Upload failed", source, err.Error()) + continue + } + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..c67d3e5 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import cli "github.com/eu-erwin/nextcloud-cli/cmd" + +func main() { + cli.Run() +} From 12855e115024df458682ceea4173aba3c3b7ba2e Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 22:01:47 +0100 Subject: [PATCH 09/15] Add github workflows --- .github/workflows/main.yml | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..bb8684a --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,51 @@ +name: Go package + +on: [push] + +jobs: + test: + runs-on: ubuntu-latest + services: + postgres: + image: nextcloud:latest + ports: + - 18080:80 + env: + SQLITE_DATABASE: nextcloud + NEXTCLOUD_ADMIN_USER: admin + NEXTCLOUD_ADMIN_PASSWORD: password + + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: 1.17 + + - name: Install dependencies + run: | + go version + go get -u golang.org/x/lint/golint + + - name: Test + run: go test -v ./... + + - name: Run vet & lint + run: | + go vet . + golint . + + build: + needs: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: 1.19 + + - name: Build + run: go build . \ No newline at end of file From bb0ca2a7fc1e27ea7f1177266da78fb9c4c2b649 Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 22:56:00 +0100 Subject: [PATCH 10/15] Move old test data to relevant package --- pkg/nextcloud/test/testdata/Folder/test.txt | 1 + pkg/nextcloud/test/testdata/test.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 pkg/nextcloud/test/testdata/Folder/test.txt create mode 100644 pkg/nextcloud/test/testdata/test.txt diff --git a/pkg/nextcloud/test/testdata/Folder/test.txt b/pkg/nextcloud/test/testdata/Folder/test.txt new file mode 100644 index 0000000..980a0d5 --- /dev/null +++ b/pkg/nextcloud/test/testdata/Folder/test.txt @@ -0,0 +1 @@ +Hello World! diff --git a/pkg/nextcloud/test/testdata/test.txt b/pkg/nextcloud/test/testdata/test.txt new file mode 100644 index 0000000..980a0d5 --- /dev/null +++ b/pkg/nextcloud/test/testdata/test.txt @@ -0,0 +1 @@ +Hello World! From 736118aabc6123455b7dc140c6e34bf2ad9a5675 Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 22:56:23 +0100 Subject: [PATCH 11/15] Clean up old tests --- test/docker/docker-compose.yml | 16 ---------------- test/testdata/Folder/test.txt | 1 - test/testdata/test.txt | 1 - 3 files changed, 18 deletions(-) delete mode 100644 test/docker/docker-compose.yml delete mode 100644 test/testdata/Folder/test.txt delete mode 100644 test/testdata/test.txt diff --git a/test/docker/docker-compose.yml b/test/docker/docker-compose.yml deleted file mode 100644 index ed9c1fb..0000000 --- a/test/docker/docker-compose.yml +++ /dev/null @@ -1,16 +0,0 @@ -version: "3" - -services: - - remogatto_cloud_test: - restart: always - image: nextcloud:latest - container_name: nextcloud_cloud_test - # volumes: - # - ./custom_apps:/var/www/html/ - environment: - - SQLITE_DATABASE=nextcloud - - NEXTCLOUD_ADMIN_USER=admin - - NEXTCLOUD_ADMIN_PASSWORD=password - ports: - - 18080:80 diff --git a/test/testdata/Folder/test.txt b/test/testdata/Folder/test.txt deleted file mode 100644 index 980a0d5..0000000 --- a/test/testdata/Folder/test.txt +++ /dev/null @@ -1 +0,0 @@ -Hello World! diff --git a/test/testdata/test.txt b/test/testdata/test.txt deleted file mode 100644 index 980a0d5..0000000 --- a/test/testdata/test.txt +++ /dev/null @@ -1 +0,0 @@ -Hello World! From d44ddeac28aa387a02e19088ad6ce09562b852e4 Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 23:27:33 +0100 Subject: [PATCH 12/15] Fix lint --- .github/workflows/main.yml | 16 +++++++--------- pkg/nextcloud/cloud_test.go | 22 +++++++++++++++++----- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bb8684a..221e29f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -23,19 +23,17 @@ jobs: with: go-version: 1.17 - - name: Install dependencies - run: | - go version - go get -u golang.org/x/lint/golint + - name: Run vet + run: go vet ./... + + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.29 - name: Test run: go test -v ./... - - name: Run vet & lint - run: | - go vet . - golint . - build: needs: test runs-on: ubuntu-latest diff --git a/pkg/nextcloud/cloud_test.go b/pkg/nextcloud/cloud_test.go index a23ae15..53ed50f 100644 --- a/pkg/nextcloud/cloud_test.go +++ b/pkg/nextcloud/cloud_test.go @@ -53,12 +53,13 @@ func (t *testSuite) TestDelete() { } func (t *testSuite) TestDownloadUpload() { - err := client.Mkdir("Test") - t.Nil(err) + mkdirErr := client.Mkdir("Test") + t.Nil(mkdirErr) - src, err := os.ReadFile(filepath.Join(testDir, "test.txt")) - err = client.Upload(src, "Test/test.txt") - t.Nil(err) + src, readErr := os.ReadFile(filepath.Join(testDir, "test.txt")) + t.Nil(readErr) + readErr = client.Upload(src, "Test/test.txt") + t.Nil(readErr) data, err := client.Download("Test/test.txt") t.Nil(err) @@ -135,10 +136,12 @@ func (t *testSuite) TestGetShare() { result, err := client.CreateFileDropShare("ShareTest") t.Nil(err) + t.Not(t.Nil(result)) result, err = client.GetShare("ShareTest") t.Nil(err) t.True(len(result.Elements) > 0) + t.Not(t.Nil(result)) err = client.Delete("ShareTest") if err != nil { @@ -152,22 +155,27 @@ func (t *testSuite) TestDeleteShare() { result, err := client.CreateFileDropShare("ShareTest") t.Nil(err) + t.Not(t.Nil(result)) result, err = client.GetShare("ShareTest") t.Nil(err) t.True(len(result.Elements) > 0) + t.Not(t.Nil(result)) result, err = client.DeleteShare(result.Elements[0].Id) t.Nil(err) + t.Not(t.Nil(result)) result, err = client.GetShare("ShareTest") t.Nil(err) t.True(len(result.Elements) == 0) + t.Not(t.Nil(result)) err = client.Delete("ShareTest") if err != nil { panic("can't delete ShareTest on TestDeleteShare") } + _ = result } func (t *testSuite) TestCreateReadOnlyShare() { @@ -176,17 +184,21 @@ func (t *testSuite) TestCreateReadOnlyShare() { result, err := client.CreateReadOnlyShare("ShareTest") t.Nil(err) + t.Not(t.Nil(result)) result, err = client.GetShare("ShareTest") t.Nil(err) t.True(len(result.Elements) > 0) + t.Not(t.Nil(result)) result, err = client.DeleteShare(result.Elements[0].Id) t.Nil(err) + t.Not(t.Nil(result)) result, err = client.GetShare("ShareTest") t.Nil(err) t.True(len(result.Elements) == 0) + t.Not(t.Nil(result)) err = client.Delete("ShareTest") if err != nil { From 16996b2a80e28d2e393868fef2426aa775b3c84a Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 23:30:48 +0100 Subject: [PATCH 13/15] Add static check --- .github/workflows/main.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 221e29f..090a173 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -31,6 +31,10 @@ jobs: with: version: v1.29 + - uses: dominikh/staticcheck-action@v1.2.0 + with: + version: "2022.1.1" + - name: Test run: go test -v ./... From 88dab1d5a4c94b2506df9a4a13723609e672df85 Mon Sep 17 00:00:00 2001 From: Erwin Date: Fri, 24 Feb 2023 23:48:06 +0100 Subject: [PATCH 14/15] Fix yoda condition --- cmd/cli.go | 10 +++++----- pkg/nextcloud/cloud.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/cli.go b/cmd/cli.go index 6c173a5..509f6e2 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -50,7 +50,7 @@ Ex.: nextcloud-cli upload --username=john --password=supersecret --url=https://c func Run() { args := flag.Args() - if 0 == len(args) { + if len(args) == 0 { log.Println(`missing command`) printHelp() return @@ -70,21 +70,21 @@ func upload(sources ...string) { fmt.Printf("Uploading %s\r\n", source) content, err := os.ReadFile(strings.Join([]string{wd, source}, "/")) - if nil != err { + if err != nil { log.Println("Can't upload", source, err.Error()) continue } target := source - if "" != *targetPath { - if err = client.Mkdir(*targetPath); nil == err { + if *targetPath != "" { + if err = client.Mkdir(*targetPath); err != nil { log.Println("New directory created", source) } target = strings.Join([]string{*targetPath, source}, "/") } err = client.Upload(content, target) - if nil != err { + if err != nil { log.Println("Upload failed", source, err.Error()) continue } diff --git a/pkg/nextcloud/cloud.go b/pkg/nextcloud/cloud.go index 69863ae..9aa5e75 100644 --- a/pkg/nextcloud/cloud.go +++ b/pkg/nextcloud/cloud.go @@ -20,10 +20,10 @@ func NewStorage( username, password string, ) (cloud.Storage, error) { - if "" == cloudUrl { + if cloudUrl == "" { return nil, errors.New("missing url") } - if "" == username || "" == password { + if username == "" || password == "" { return nil, errors.New("missing credentials") } From 7919bb330f142eaaf347333100e6b4175e82439c Mon Sep 17 00:00:00 2001 From: Erwin Date: Sat, 25 Feb 2023 00:01:15 +0100 Subject: [PATCH 15/15] Workflows on PR and master branch only --- .github/workflows/main.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 090a173..92be835 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,6 +1,10 @@ name: Go package -on: [push] +on: + pull_request: + push: + branches: + - master jobs: test: