Initial Commit
Internal Link Generation
This commit is contained in:
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
|
||||
}
|
Reference in New Issue
Block a user