Initial Commit
Internal Link Generation
This commit is contained in:
commit
e865313e15
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
ow
|
||||||
|
build/
|
11
Makefile
Normal file
11
Makefile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
nccli:
|
||||||
|
go build -o build/nccli
|
||||||
|
|
||||||
|
run: ./build/nccli
|
||||||
|
cd build && ./nccli
|
||||||
|
|
||||||
|
install:
|
||||||
|
go install
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm build/nccli
|
76
cmd/link.go
Normal file
76
cmd/link.go
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
|
||||||
|
*/
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.bullercodeworks.com/brian/nccli/util"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
// linkCmd represents the link command
|
||||||
|
var linkCmd = &cobra.Command{
|
||||||
|
Use: "link",
|
||||||
|
Short: "Generate a link for a file",
|
||||||
|
RunE: runLinkCmd,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(linkCmd)
|
||||||
|
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
// and all subcommands, e.g.:
|
||||||
|
// linkCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
|
// Cobra supports local flags which will only run when this command
|
||||||
|
// is called directly, e.g.:
|
||||||
|
// linkCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
}
|
||||||
|
|
||||||
|
func runLinkCmd(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) == 0 {
|
||||||
|
return errors.New("No argument passed")
|
||||||
|
}
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
root := viper.GetString("directory")
|
||||||
|
var resp []string
|
||||||
|
for _, file := range args {
|
||||||
|
fileName := file
|
||||||
|
file = strings.TrimPrefix(file, "./")
|
||||||
|
if !strings.HasPrefix(file, "/") {
|
||||||
|
// Make sure that we are in the NC root
|
||||||
|
if !strings.HasPrefix(cwd, root) {
|
||||||
|
return errors.New("File is not in your Nextcloud Directory")
|
||||||
|
}
|
||||||
|
// Go ahead and append the nextcloud root to this file
|
||||||
|
file = fmt.Sprintf("%s/%s", cwd, file)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) {
|
||||||
|
resp = append(resp, fmt.Sprintf("%s: File does not exist.", fileName))
|
||||||
|
} else {
|
||||||
|
file = util.NormalizeLocalPath(file)
|
||||||
|
fileId, err := util.GetFileId(util.GetFilesUrl(file))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Printf("%s/f/%s\n", viper.GetString("ncurl"), fileId)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, r := range resp {
|
||||||
|
fmt.Println(r)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
142
cmd/root.go
Normal file
142
cmd/root.go
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
|
||||||
|
*/
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.bullercodeworks.com/brian/nccli/util"
|
||||||
|
gap "github.com/muesli/go-app-paths"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
// rootCmd represents the base command when called without any subcommands
|
||||||
|
var (
|
||||||
|
Version = "1.0"
|
||||||
|
Build = "1"
|
||||||
|
configFile string
|
||||||
|
rootCmd = &cobra.Command{
|
||||||
|
Use: "nccli",
|
||||||
|
Short: "A small utility for various Nextcloud functions",
|
||||||
|
// Uncomment the following line if your bare application
|
||||||
|
// has an action associated with it:
|
||||||
|
// Run: func(cmd *cobra.Command, args []string) { },
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||||
|
func Execute() {
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.Version = Version
|
||||||
|
initConfig()
|
||||||
|
}
|
||||||
|
|
||||||
|
func initConfig() {
|
||||||
|
var firstDir string // In case we need to make directories
|
||||||
|
if configFile != "" {
|
||||||
|
viper.SetConfigFile(configFile)
|
||||||
|
} else {
|
||||||
|
scope := gap.NewScope(gap.User, "nccli")
|
||||||
|
dirs, err := scope.ConfigDirs()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Can't retrieve default config. Please manually pass a config file with '--config'")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
firstDir = dirs[0]
|
||||||
|
for _, v := range dirs {
|
||||||
|
viper.AddConfigPath(v)
|
||||||
|
}
|
||||||
|
viper.SetConfigName("nccli")
|
||||||
|
viper.SetConfigType("yaml")
|
||||||
|
}
|
||||||
|
var createConfig bool
|
||||||
|
v2Path := fmt.Sprintf("%s%s%s", firstDir, string(os.PathSeparator), "nccli.yaml")
|
||||||
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
|
createConfig = true
|
||||||
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||||
|
// Config file not found; Check if we have a v1 config file
|
||||||
|
fmt.Println("Config file not found.")
|
||||||
|
} else {
|
||||||
|
fmt.Println("Found config file, but another error occurred.")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if createConfig {
|
||||||
|
_, err := os.Stat(firstDir)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err := os.Mkdir(firstDir, 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err = viper.WriteConfigAs(v2Path); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ncuser := viper.GetString("ncuser")
|
||||||
|
if ncuser == "" {
|
||||||
|
ncuser = util.PromptUser("Nextcloud User Name")
|
||||||
|
if ncuser == "" {
|
||||||
|
fmt.Println("No Nextcloud User Name given")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
viper.Set("ncuser", ncuser)
|
||||||
|
viper.WriteConfig()
|
||||||
|
}
|
||||||
|
ncpw := viper.GetString("ncpw")
|
||||||
|
if ncpw == "" {
|
||||||
|
var err error
|
||||||
|
ncpw = util.PromptUser("Enter Password")
|
||||||
|
if ncpw != "" {
|
||||||
|
ncpw, err = util.Encrypt(ncpw)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error encrypting password: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println("No Nextcloud Password given")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
viper.Set("ncpw", ncpw)
|
||||||
|
viper.WriteConfig()
|
||||||
|
}
|
||||||
|
url := viper.GetString("ncurl")
|
||||||
|
if url == "" {
|
||||||
|
url = util.PromptUser("Nextcloud Base URL")
|
||||||
|
if url == "" {
|
||||||
|
fmt.Println("No Nextcloud Base URL given")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if !strings.HasSuffix(url, "/") {
|
||||||
|
url = url + "/"
|
||||||
|
}
|
||||||
|
viper.Set("ncurl", url)
|
||||||
|
viper.WriteConfig()
|
||||||
|
}
|
||||||
|
dir := viper.GetString("directory")
|
||||||
|
if dir == "" {
|
||||||
|
dir = util.PromptUser("Path to your local Nextcloud Root")
|
||||||
|
if dir == "" {
|
||||||
|
fmt.Println("No path to Nexctloud root given")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if !strings.HasSuffix(dir, "/") {
|
||||||
|
dir = dir + "/"
|
||||||
|
}
|
||||||
|
viper.Set("directory", dir)
|
||||||
|
viper.WriteConfig()
|
||||||
|
}
|
||||||
|
}
|
33
go.mod
Normal file
33
go.mod
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
module git.bullercodeworks.com/brian/nccli
|
||||||
|
|
||||||
|
go 1.22.3
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/muesli/go-app-paths v0.2.2
|
||||||
|
github.com/spf13/cobra v1.8.0
|
||||||
|
github.com/spf13/viper v1.18.2
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||||
|
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
|
github.com/magiconair/properties v1.8.7 // indirect
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||||
|
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||||
|
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
|
||||||
|
github.com/sagikazarmark/locafero v0.4.0 // indirect
|
||||||
|
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
|
||||||
|
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||||
|
github.com/spf13/afero v1.11.0 // indirect
|
||||||
|
github.com/spf13/cast v1.6.0 // indirect
|
||||||
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
|
github.com/subosito/gotenv v1.6.0 // indirect
|
||||||
|
go.uber.org/atomic v1.9.0 // indirect
|
||||||
|
go.uber.org/multierr v1.9.0 // indirect
|
||||||
|
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
|
||||||
|
golang.org/x/sys v0.15.0 // indirect
|
||||||
|
golang.org/x/text v0.14.0 // indirect
|
||||||
|
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
62
go.sum
Normal file
62
go.sum
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
|
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/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||||
|
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||||
|
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||||
|
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
|
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||||
|
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||||
|
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/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||||
|
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||||
|
github.com/muesli/go-app-paths v0.2.2 h1:NqG4EEZwNIhBq/pREgfBmgDmt3h1Smr1MjZiXbpZUnI=
|
||||||
|
github.com/muesli/go-app-paths v0.2.2/go.mod h1:SxS3Umca63pcFcLtbjVb+J0oD7cl4ixQWoBKhGEtEho=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
|
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
|
||||||
|
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
|
||||||
|
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
||||||
|
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
||||||
|
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||||
|
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||||
|
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
|
||||||
|
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
|
||||||
|
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
|
||||||
|
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||||
|
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
|
||||||
|
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
|
||||||
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
|
||||||
|
github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||||
|
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||||
|
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||||
|
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||||
|
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
||||||
|
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
||||||
|
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
|
||||||
|
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
|
||||||
|
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
||||||
|
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||||
|
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
10
main.go
Normal file
10
main.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
|
||||||
|
*/
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "git.bullercodeworks.com/brian/nccli/cmd"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cmd.Execute()
|
||||||
|
}
|
62
util/cli.go
Normal file
62
util/cli.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
||||||
|
*/
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PromptUser(text string) string {
|
||||||
|
var resp string
|
||||||
|
fmt.Println(text)
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
if scanner.Scan() {
|
||||||
|
resp = scanner.Text()
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
func PromptUserOptTrimmed(text string, def string) string {
|
||||||
|
var resp string
|
||||||
|
if def == "" {
|
||||||
|
resp = strings.TrimSpace(PromptUser(text))
|
||||||
|
} else {
|
||||||
|
resp = strings.TrimSpace(PromptUser(fmt.Sprintf("%s [%s]:", text, def)))
|
||||||
|
if resp == "" {
|
||||||
|
resp = def
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for resp == "" {
|
||||||
|
resp = strings.TrimSpace(PromptUser(text))
|
||||||
|
if resp == "" {
|
||||||
|
fmt.Println("Cannot be blank")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
func PromptUserTrimmed(text string) string {
|
||||||
|
return PromptUserOptTrimmed(text, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func PromptUserInt(text string) int {
|
||||||
|
var resp string
|
||||||
|
var i int
|
||||||
|
for resp == "" {
|
||||||
|
resp = strings.TrimSpace(PromptUser(text))
|
||||||
|
if resp == "" {
|
||||||
|
fmt.Println("Cannot be blank")
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
i, err = strconv.Atoi(resp)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Must be a number")
|
||||||
|
resp = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i
|
||||||
|
}
|
62
util/crypto.go
Normal file
62
util/crypto.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getKey() string {
|
||||||
|
key := []byte("4bUMVpSJjvjwEC7qV2AQjvjwEC7qV2AQ")
|
||||||
|
return hex.EncodeToString(key)
|
||||||
|
}
|
||||||
|
func Encrypt(input string) (string, error) {
|
||||||
|
btKey, _ := hex.DecodeString(getKey())
|
||||||
|
plaintext := []byte(input)
|
||||||
|
|
||||||
|
// Create a new Cipher Block from the key
|
||||||
|
block, err := aes.NewCipher(btKey)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
||||||
|
iv := ciphertext[:aes.BlockSize]
|
||||||
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
stream := cipher.NewCFBEncrypter(block, iv)
|
||||||
|
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
|
||||||
|
|
||||||
|
// Convert to base64
|
||||||
|
return base64.URLEncoding.EncodeToString(ciphertext), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Decrypt(input string) (string, error) {
|
||||||
|
btKey, _ := hex.DecodeString(getKey())
|
||||||
|
ciphertext, _ := base64.URLEncoding.DecodeString(input)
|
||||||
|
|
||||||
|
block, err := aes.NewCipher(btKey)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ciphertext) < aes.BlockSize {
|
||||||
|
return "", errors.New("ciphertext is too short")
|
||||||
|
}
|
||||||
|
iv := ciphertext[:aes.BlockSize]
|
||||||
|
ciphertext = ciphertext[aes.BlockSize:]
|
||||||
|
|
||||||
|
stream := cipher.NewCFBDecrypter(block, iv)
|
||||||
|
|
||||||
|
// XORKeyStream can work in-place if the two arguments are the same.
|
||||||
|
stream.XORKeyStream(ciphertext, ciphertext)
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s", ciphertext), nil
|
||||||
|
}
|
71
util/nextcloud.go
Normal file
71
util/nextcloud.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
|
||||||
|
*/
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetAuth() (string, error) {
|
||||||
|
username := viper.GetString("ncuser")
|
||||||
|
password, err := Decrypt(viper.GetString("ncpw"))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
auth := username + ":" + password
|
||||||
|
return base64.StdEncoding.EncodeToString([]byte(auth)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NormalizeLocalPath(path string) string {
|
||||||
|
root := viper.GetString("directory")
|
||||||
|
return strings.TrimPrefix(path, root)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetFilesUrl(path string) string {
|
||||||
|
return fmt.Sprintf("%sremote.php/dav/files/%s/%s", viper.GetString("ncurl"), viper.GetString("ncuser"), path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetFileId(url string) (string, error) {
|
||||||
|
var auth string
|
||||||
|
client := &http.Client{}
|
||||||
|
data := bytes.NewBuffer([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<d:propfind xmlns:d="DAV:">
|
||||||
|
<d:prop xmlns:oc="http://owncloud.org/ns">
|
||||||
|
<oc:fileid/>
|
||||||
|
</d:prop>
|
||||||
|
</d:propfind>`))
|
||||||
|
req, err := http.NewRequest("PROPFIND", url, data)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
auth, err = GetAuth()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
req.Header.Add("Authorization", "Basic "+auth)
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
var body []byte
|
||||||
|
body, err = io.ReadAll(resp.Body)
|
||||||
|
wrkArr := strings.Split(string(body), "<oc:fileid>")
|
||||||
|
if len(wrkArr) != 2 {
|
||||||
|
return "", errors.New("Couldn't find File ID")
|
||||||
|
}
|
||||||
|
wrkArr = strings.Split(wrkArr[1], "</oc:fileid>")
|
||||||
|
if len(wrkArr) != 2 {
|
||||||
|
return "", errors.New("Couldn't find File ID")
|
||||||
|
}
|
||||||
|
return wrkArr[0], nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user