Initial Commit

Internal Link Generation
This commit is contained in:
2024-05-28 16:50:11 -05:00
commit e865313e15
11 changed files with 531 additions and 0 deletions

76
cmd/link.go Normal file
View 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
View 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()
}
}