Rework input methods
This commit is contained in:
165
main.go
165
main.go
@@ -2,11 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,63 +26,164 @@ const TargetColor = Yellow
|
|||||||
const ErrorColor = Red
|
const ErrorColor = Red
|
||||||
const DefaultColor = White
|
const DefaultColor = White
|
||||||
|
|
||||||
|
var programName = os.Args[0]
|
||||||
|
|
||||||
type LinkInstruction struct {
|
type LinkInstruction struct {
|
||||||
Source string
|
Source string
|
||||||
Target string
|
Target string
|
||||||
Force bool
|
Force bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func InstructionToString(instruction LinkInstruction) string {
|
|
||||||
return fmt.Sprintf("%s%s%s%s%s%s%s%s%s", SourceColor, instruction.Source, DefaultColor, deliminer, TargetColor, instruction.Target, DefaultColor, deliminer, strconv.FormatBool(instruction.Force))
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Format:
|
// Format:
|
||||||
// source|target|force?
|
// source,target,force?
|
||||||
log.SetFlags(log.Lmicroseconds)
|
log.SetFlags(log.Lmicroseconds)
|
||||||
var inputs []string
|
|
||||||
// os.Chdir("C:/Users/Administrator/Seafile/Last-Epoch-Backup")
|
|
||||||
|
|
||||||
if len(os.Args) > 1 {
|
recurse := flag.String("r", "", "recurse into directories")
|
||||||
inputs = append(inputs, os.Args[1:]...)
|
file := flag.String("f", "", "file to read instructions from")
|
||||||
} else {
|
flag.Parse()
|
||||||
info, err := os.Stdin.Stat()
|
|
||||||
|
log.Printf("Recurse: %s", *recurse)
|
||||||
|
log.Printf("File: %s", *file)
|
||||||
|
// The plan:
|
||||||
|
// As input take file or list of files (via -f)
|
||||||
|
// For every file: ch work dir to file dir
|
||||||
|
// Read file
|
||||||
|
// Parse instructions
|
||||||
|
// Run instructions
|
||||||
|
// If not -f then if -r:
|
||||||
|
// Recurse into directories
|
||||||
|
// Find all sync files
|
||||||
|
// Repeat the above for every file
|
||||||
|
// If not -f and not -r and args:
|
||||||
|
// Read from args
|
||||||
|
// Parse instructions
|
||||||
|
// Run instructions
|
||||||
|
// If not -f and not -r and no args:
|
||||||
|
// Read from stdin
|
||||||
|
// Parse instructions
|
||||||
|
// Run instructions
|
||||||
|
|
||||||
|
if *recurse != "" {
|
||||||
|
startingDir, _ := os.Getwd()
|
||||||
|
log.Println("Workdir:", startingDir)
|
||||||
|
var targetDir = os.Args[1]
|
||||||
|
if targetDir == "" {
|
||||||
|
targetDir, _ = os.Getwd()
|
||||||
|
}
|
||||||
|
log.Printf("Recursively finding sync files in workdir %s...", targetDir)
|
||||||
|
|
||||||
|
files, err := GetSyncFilesRecursively(targetDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to stat stdin: %s%+v%s", ErrorColor, err, DefaultColor)
|
log.Fatalf("Failed to get sync files recursively: %s%+v%s", ErrorColor, err, DefaultColor)
|
||||||
}
|
}
|
||||||
if info.Mode()&os.ModeNamedPipe != 0 {
|
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
dirRegex, _ := regexp.Compile("^(.+?)sync$")
|
||||||
for scanner.Scan() {
|
for _, file := range files {
|
||||||
inputs = append(inputs, scanner.Text())
|
file = NormalizePath(file)
|
||||||
|
fileDir := dirRegex.FindStringSubmatch(file)
|
||||||
|
err := os.Chdir(fileDir[1])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to change directory to %s%s%s: %s%+v%s", SourceColor, fileDir[1], DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
}
|
}
|
||||||
if err := scanner.Err(); err != nil {
|
|
||||||
log.Fatalf("Error reading from stdin: %s%+v%s", ErrorColor, err, DefaultColor)
|
}
|
||||||
|
} else {
|
||||||
|
var instructions []LinkInstruction
|
||||||
|
if *file != "" {
|
||||||
|
instructions, _ = ReadFromFile(*file)
|
||||||
|
} else if len(os.Args) > 1 {
|
||||||
|
instructions, _ = ReadFromArgs()
|
||||||
|
} else {
|
||||||
|
instructions, _ = ReadFromStdin()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(instructions) == 0 {
|
||||||
|
log.Printf("No input provided")
|
||||||
|
log.Println("Supply input as: ")
|
||||||
|
log.Printf("Arguments - %s <source>,<target>,<force?>", programName)
|
||||||
|
log.Printf("File - %s -f <file>", programName)
|
||||||
|
log.Printf("Folder (finding sync files in folder recursively) - %s -r <folder>", programName)
|
||||||
|
log.Printf("stdin - (cat <file> | %s)", programName)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
for _, instruction := range instructions {
|
||||||
|
log.Printf("Processing: %s", InstructionToString(instruction))
|
||||||
|
err := RunInstruction(instruction)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed parsing instruction %s%s%s due to %s%+v%s", SourceColor, InstructionToString(instruction), DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(inputs) == 0 {
|
}
|
||||||
log.Printf("No input provided")
|
|
||||||
log.Printf("Supply input as either arguments (source,target,force?)")
|
|
||||||
log.Printf("or via stdin (cat <file> | %s)", os.Args[0])
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, line := range inputs {
|
func ReadFromFile(input string) ([]LinkInstruction, error) {
|
||||||
instruction, err := parseLine(line)
|
log.Printf("Reading input from file: %s", input)
|
||||||
|
var instructions []LinkInstruction
|
||||||
|
file, err := os.Open(input)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to open file %s%s%s: %s%+v%s", SourceColor, input, DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
instruction, err := ParseInstruction(line)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error parsing line: %s'%s'%s, error: %s%+v%s", SourceColor, line, DefaultColor, ErrorColor, err, DefaultColor)
|
log.Printf("Error parsing line: %s'%s'%s, error: %s%+v%s", SourceColor, line, DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Printf("Processing: %s", InstructionToString(instruction))
|
instructions = append(instructions, instruction)
|
||||||
err = processInstruction(instruction)
|
}
|
||||||
|
log.Printf("Read %d instructions from file", len(instructions))
|
||||||
|
return instructions, nil
|
||||||
|
}
|
||||||
|
func ReadFromArgs() ([]LinkInstruction, error) {
|
||||||
|
log.Printf("Reading input from args")
|
||||||
|
var instructions []LinkInstruction
|
||||||
|
for _, arg := range os.Args[1:] {
|
||||||
|
instruction, err := ParseInstruction(arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed parsing instruction %s%s%s due to %s%+v%s", SourceColor, InstructionToString(instruction), DefaultColor, ErrorColor, err, DefaultColor)
|
log.Printf("Error parsing arg: %s'%s'%s, error: %s%+v%s", SourceColor, arg, DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
instructions = append(instructions, instruction)
|
||||||
}
|
}
|
||||||
|
log.Printf("Read %d instructions from args", len(instructions))
|
||||||
|
return instructions, nil
|
||||||
}
|
}
|
||||||
func parseLine(line string) (LinkInstruction, error) {
|
func ReadFromStdin() ([]LinkInstruction, error) {
|
||||||
|
log.Printf("Reading input from stdin")
|
||||||
|
var instructions []LinkInstruction
|
||||||
|
|
||||||
|
info, err := os.Stdin.Stat()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to stat stdin: %s%+v%s", ErrorColor, err, DefaultColor)
|
||||||
|
}
|
||||||
|
if info.Mode()&os.ModeNamedPipe != 0 {
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
instruction, err := ParseInstruction(line)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error parsing line: %s'%s'%s, error: %s%+v%s", SourceColor, line, DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
instructions = append(instructions, instruction)
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
log.Fatalf("Error reading from stdin: %s%+v%s", ErrorColor, err, DefaultColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Read %d instructions from stdin", len(instructions))
|
||||||
|
return instructions, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseInstruction(line string) (LinkInstruction, error) {
|
||||||
parts := strings.Split(line, deliminer)
|
parts := strings.Split(line, deliminer)
|
||||||
instruction := LinkInstruction{}
|
instruction := LinkInstruction{}
|
||||||
|
|
||||||
@@ -107,7 +208,7 @@ func parseLine(line string) (LinkInstruction, error) {
|
|||||||
return instruction, nil
|
return instruction, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processInstruction(instruction LinkInstruction) error {
|
func RunInstruction(instruction LinkInstruction) error {
|
||||||
if !FileExists(instruction.Source) {
|
if !FileExists(instruction.Source) {
|
||||||
return fmt.Errorf("instruction source %s%s%s does not exist", SourceColor, instruction.Source, DefaultColor)
|
return fmt.Errorf("instruction source %s%s%s does not exist", SourceColor, instruction.Source, DefaultColor)
|
||||||
}
|
}
|
||||||
|
2
sync
2
sync
@@ -1 +1 @@
|
|||||||
"Last Epoch","C:\Users\Administrator\AppData\LocalLow\Eleventh Hour Games\Last Epoch"
|
test,testdir/test3
|
32
util.go
32
util.go
@@ -2,8 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,10 +22,7 @@ func IsSymlink(path string) (bool, error) {
|
|||||||
|
|
||||||
func FileExists(path string) bool {
|
func FileExists(path string) bool {
|
||||||
_, err := os.Lstat(path)
|
_, err := os.Lstat(path)
|
||||||
if err != nil {
|
return err == nil
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NormalizePath(input string) string {
|
func NormalizePath(input string) string {
|
||||||
@@ -62,3 +61,28 @@ func ConvertHome(input string) (string, error) {
|
|||||||
}
|
}
|
||||||
return input, nil
|
return input, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InstructionToString(instruction LinkInstruction) string {
|
||||||
|
return fmt.Sprintf("%s%s%s%s%s%s%s%s%s", SourceColor, instruction.Source, DefaultColor, deliminer, TargetColor, instruction.Target, DefaultColor, deliminer, strconv.FormatBool(instruction.Force))
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSyncFilesRecursively(input string) ([]string, error) {
|
||||||
|
var files []string
|
||||||
|
err := filepath.WalkDir(input, func(path string, file fs.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Effectively only find files named "sync" (with no extension!!)
|
||||||
|
if !file.IsDir() && strings.HasSuffix(path, "sync") {
|
||||||
|
files = append(files, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return files, nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user