Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add cli command on top #1

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
57 changes: 57 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Go package

on:
pull_request:
push:
branches:
- master

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: Run vet
run: go vet ./...

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.29

- uses: dominikh/[email protected]
with:
version: "2022.1.1"

- name: Test
run: go test -v ./...

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 .
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*~

.idea
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
92 changes: 92 additions & 0 deletions cmd/cli.go
Original file line number Diff line number Diff line change
@@ -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 len(args) == 0 {
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 err != nil {
log.Println("Can't upload", source, err.Error())
continue
}

target := source
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 err != nil {
log.Println("Upload failed", source, err.Error())
continue
}
}
}
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import cli "github.com/eu-erwin/nextcloud-cli/cmd"

func main() {
cli.Run()
}
35 changes: 35 additions & 0 deletions pkg/cloud/storage.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading