Created
July 25, 2023 11:30
-
-
Save boynoiz/c373ed01decb48a5ed10f103b0a25f6f to your computer and use it in GitHub Desktop.
🤪
This file contains hidden or 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
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