Allow non-existant files in command-line args

This commit is contained in:
Luke I. Wilson 2021-03-20 13:36:26 -05:00
parent 6e97e8345a
commit ca4a847547

35
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io/fs" "io/fs"
"io/ioutil" "io/ioutil"
@ -47,22 +48,32 @@ func main() {
panic(err) panic(err)
} }
// Load files from command-line arguments // Open files from command-line arguments
if len(os.Args) > 1 { if len(os.Args) > 1 {
for _, path := range os.Args[1:] { for i := 1; i < len(os.Args); i++ {
file, err := os.Open(path) _, err := os.Stat(os.Args[i])
if err != nil {
panic("File could not be opened at path " + path)
}
defer file.Close()
bytes, err := ioutil.ReadAll(file) var dirty bool
if err != nil { var bytes []byte
panic("Could not read all of " + path)
if errors.Is(err, os.ErrNotExist) { // If the file does not exist...
dirty = true
} else { // If the file exists...
file, err := os.Open(os.Args[i])
if err != nil {
panic("File could not be opened at path " + os.Args[i])
}
defer file.Close()
bytes, err = ioutil.ReadAll(file)
if err != nil {
panic("Could not read all of " + os.Args[i])
}
} }
textEdit := ui.NewTextEdit(&s, path, string(bytes), &theme) textEdit := ui.NewTextEdit(&s, os.Args[i], string(bytes), &theme)
tabContainer.AddTab(path, textEdit) textEdit.Dirty = dirty
tabContainer.AddTab(os.Args[i], textEdit)
} }
} }