This repository has been archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
796 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Copyright © 2024 Simon Bråten <[email protected]> | ||
This file is part of yapc-cli | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// configCmd represents the config command | ||
var configCmd = &cobra.Command{ | ||
Use: "config", | ||
Short: "A brief description of your command", | ||
Long: `A longer description that spans multiple lines and likely contains examples | ||
and usage of using your command. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("config called") | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(configCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// configCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright © 2024 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
// Used for flags. | ||
cfgFile *string | ||
) | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "yapc-cli", | ||
Short: "Simple CLI for YAPC", | ||
Long: `A CLI for uploading files to YAPC.`, | ||
} | ||
|
||
// 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() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
cfgFile = rootCmd.PersistentFlags().StringP("config", "c", "", "config file (default is $HOME/.yapc.yaml)") | ||
} | ||
|
||
func initConfig() { | ||
if *cfgFile != "" { | ||
// Use config file from the flag. | ||
viper.SetConfigFile(*cfgFile) | ||
} else { | ||
// Find home directory. | ||
home, err := os.UserHomeDir() | ||
cobra.CheckErr(err) | ||
|
||
viper.AddConfigPath(home) | ||
viper.AddConfigPath(".") | ||
viper.SetConfigType("yaml") | ||
viper.SetConfigName(".yapc") | ||
|
||
viper.SetDefault("Endpoint", "https://pomf1.080609.xyz") | ||
} | ||
|
||
viper.AutomaticEnv() | ||
|
||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Println("Using config file:", viper.ConfigFileUsed()) | ||
} else { | ||
fmt.Println(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
Copyright © 2024 Simon Bråten <[email protected]> | ||
This file is part of yapc-cli | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"mime/multipart" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
endpoint string | ||
) | ||
|
||
// uploadCmd represents the upload command | ||
var uploadCmd = &cobra.Command{ | ||
Use: "upload [file...]", | ||
Short: "Upload a file", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
for _, path := range args { | ||
if err := uploadFileOrDir(path); err != nil { | ||
fmt.Printf("Error uploading %s: %v\n", path, err) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(uploadCmd) | ||
|
||
// Find and load the config file | ||
initConfig() | ||
|
||
// Get the endpoint from the config | ||
endpoint = viper.GetString("Endpoint") | ||
} | ||
|
||
func uploadFileOrDir(path string) error { | ||
fileInfo, err := os.Stat(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if fileInfo.IsDir() { | ||
// If it's a directory, upload all files within it | ||
files, err := os.ReadDir(path) | ||
if err != nil { | ||
return err | ||
} | ||
for _, file := range files { | ||
if err := uploadFileOrDir(filepath.Join(path, file.Name())); err != nil { | ||
return err | ||
} | ||
} | ||
} else { | ||
// If it's a file, upload it | ||
uploadFile(path) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func uploadFile(path string) { | ||
// Open the file | ||
file, err := os.Open(path) | ||
if err != nil { | ||
fmt.Printf("Error opening file %s: %v\n", path, err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
// Create a buffer to store our request body | ||
var requestBody bytes.Buffer | ||
writer := multipart.NewWriter(&requestBody) | ||
|
||
// Create a form file field | ||
part, err := writer.CreateFormFile("file", filepath.Base(path)) | ||
if err != nil { | ||
fmt.Printf("Error creating form file: %v\n", err) | ||
return | ||
} | ||
|
||
// Copy the file content to the form file field | ||
_, err = io.Copy(part, file) | ||
if err != nil { | ||
fmt.Printf("Error copying file content: %v\n", err) | ||
return | ||
} | ||
|
||
// Close the multipart writer | ||
err = writer.Close() | ||
if err != nil { | ||
fmt.Printf("Error closing multipart writer: %v\n", err) | ||
return | ||
} | ||
|
||
// Create a new HTTP request | ||
req, err := http.NewRequest("POST", endpoint+"/store", &requestBody) | ||
if err != nil { | ||
fmt.Printf("Error creating HTTP request: %v\n", err) | ||
return | ||
} | ||
|
||
// Set the content type header | ||
req.Header.Set("Content-Type", writer.FormDataContentType()) | ||
|
||
// Send the request | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
fmt.Printf("Error sending HTTP request: %v\n", err) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
type respJson struct { | ||
SHA256 string `json:"sha256"` | ||
SHA1 string `json:"sha1"` | ||
MD5 string `json:"md5"` | ||
CRC32 string `json:"crc32"` | ||
} | ||
|
||
// Declare a variable of type respJson | ||
var respData respJson | ||
|
||
// Decode the response into the respData variable | ||
err = json.NewDecoder(resp.Body).Decode(&respData) | ||
if err != nil { | ||
fmt.Printf("Error decoding response: %v\n", err) | ||
return | ||
} | ||
|
||
// Now you can access the SHA256 field from respData | ||
fmt.Printf("Uploaded %s: %s\n", path, respData.SHA256) | ||
} | ||
|
||
type uploadModel struct { | ||
progress float64 | ||
} | ||
|
||
func (m *uploadModel) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m *uploadModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
if msg.Type == tea.KeyEnter { | ||
return m, tea.Quit | ||
} | ||
} | ||
return m, nil | ||
} | ||
|
||
func (m *uploadModel) View() string { | ||
return fmt.Sprintf("Uploading... %0.2f%%\n", m.progress*100) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module github.com/hexahigh/yapc/cli | ||
|
||
go 1.22.1 | ||
|
||
require ( | ||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect | ||
github.com/charmbracelet/bubbletea v0.25.0 // indirect | ||
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect | ||
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/lucasb-eyer/go-colorful v1.2.0 // indirect | ||
github.com/magiconair/properties v1.8.7 // indirect | ||
github.com/mattn/go-isatty v0.0.18 // indirect | ||
github.com/mattn/go-localereader v0.0.1 // indirect | ||
github.com/mattn/go-runewidth v0.0.14 // indirect | ||
github.com/mitchellh/mapstructure v1.5.0 // indirect | ||
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect | ||
github.com/muesli/cancelreader v0.2.2 // indirect | ||
github.com/muesli/reflow v0.3.0 // indirect | ||
github.com/muesli/termenv v0.15.2 // indirect | ||
github.com/pelletier/go-toml/v2 v2.1.0 // indirect | ||
github.com/rivo/uniseg v0.2.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/cobra v1.8.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/spf13/viper v1.18.2 // 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/sync v0.5.0 // indirect | ||
golang.org/x/sys v0.15.0 // indirect | ||
golang.org/x/term v0.6.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 | ||
) |
Oops, something went wrong.