bbash/command/mv/mv.go
Tobias P.L Wennberg e239096ef7 Fix merge
2025-01-12 09:35:41 +01:00

48 lines
954 B
Go

package mv
import (
"bbash/environment"
"bbash/input_parser"
"fmt"
"os"
"path/filepath"
"strings"
)
func Mv(in input_parser.Input, env *environment.Env) {
args := strings.SplitN(in.Args_raw, " ", 2)
if len(args) != 2 {
fmt.Print("No right amount of args")
return
}
file_path_source := filepath.Join(env.Path, args[0])
file_path_dest := filepath.Join(env.Path, args[1])
source, err := os.ReadFile(file_path_source)
if err != nil {
fmt.Println(fmt.Sprintf("Error opening file: %s", err.Error()))
return
}
dest, err := os.Create(file_path_dest)
defer dest.Close()
if err != nil {
fmt.Println(fmt.Sprintf("Error opening file: %s", err.Error()))
return
}
_, err = dest.Write(source)
if err != nil {
fmt.Println(fmt.Sprintf("Error writing destination: %s", err.Error()))
return
}
err = os.Remove(file_path_source)
if err != nil {
fmt.Println(fmt.Sprintf("Error removing source: %s", err.Error()))
return
}
}