Skip to content

Instantly share code, notes, and snippets.

@boynoiz
Created July 25, 2023 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boynoiz/c373ed01decb48a5ed10f103b0a25f6f to your computer and use it in GitHub Desktop.
Save boynoiz/c373ed01decb48a5ed10f103b0a25f6f to your computer and use it in GitHub Desktop.
🤪
package main
import (
"bytes"
"fmt"
"gopkg.in/yaml.v3"
"log"
"os"
"os/exec"
"path/filepath"
)
var configFile = "upload.yaml"
type Config struct {
AwsProfile string `yaml:"aws_profile"`
ExcludeDir []string `yaml:"exclude_dir"`
FrontendList []FrontendList `yaml:"frontend_list"`
}
type FrontendList struct {
BucketName string `yaml:"bucket_name"`
DirName string `yaml:"dir_name"`
}
func main() {
data, err := ReadYAML()
if err != nil {
log.Fatalf("Could not parser the yaml file\nError: %s", err.Error())
}
}
func Uploader(awsProfile, dirName, bucketName string, opts []string) (error, string, string) {
cmd := fmt.Sprintf("s5cmd --profile %s --exlude %s cp %s %s", awsProfile, opts, dirName, bucketName)
return execShell(cmd)
}
func ReadYAML() (*Config, error) {
currentDir, err := os.Getwd()
if err != nil {
log.Fatalf("Could not get current directory\nError: %s", err.Error())
return nil, err
}
data, err := os.ReadFile(filepath.Join(currentDir, configFile))
if err != nil {
log.Fatalf("Could not read %s file\nError: %s", filepath.Join(currentDir, configFile), err.Error())
return nil, err
}
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
log.Fatalf("Could not parser the yaml file\nError: %s", err.Error())
return nil, err
}
return &config, nil
}
func execShell(command string) (error, string, string) {
stdout := bytes.Buffer{}
stderr := bytes.Buffer{}
cmd := exec.Command("bash", "-c", command)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return err, stdout.String(), stderr.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment