Skip to content

Commit

Permalink
display error messages on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zunda-arrow committed Dec 27, 2023
1 parent 7773223 commit 67a6f6b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 58 deletions.
2 changes: 1 addition & 1 deletion ansi/ansi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ansi
import "fmt"

func Clear() {
fmt.Print("\033[H\033[0J")
// fmt.Print("\033[H\033[0J")
}

func MoveCursor(line int, col int) {
Expand Down
103 changes: 59 additions & 44 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import (
)

type Config struct {
AsciiArts []AsciiArt
Sessions []Session
LastSession *LastSession
ascii *string
AsciiArts []AsciiArt
isAsciiError bool
Sessions []Session
LastSession *LastSession
ascii *string
}

type SessionType string
Expand Down Expand Up @@ -66,24 +67,15 @@ type LastSession struct {
User string
}

type Origin string

const (
Center Origin = "center"
)

type AsciiArt struct {
StrLines []string
Lines int
Cols int

Messages []string
Origin Origin

name string
name string
}

func newAsciiArt(name string, art string, messages []string, origin Origin) AsciiArt {
func newAsciiArt(name string, art string, messages []string) AsciiArt {
lines := strings.Split(art, "\n")

longestLine := utf8.RuneCountInString(lines[0])
Expand All @@ -99,7 +91,6 @@ func newAsciiArt(name string, art string, messages []string, origin Origin) Asci
Cols: longestLine,
Lines: len(lines),
Messages: messages,
Origin: origin,
name: name,
}
}
Expand All @@ -115,23 +106,22 @@ func parseAsciiFile(filename string) (*AsciiArt, error) {
contentsLines := strings.Split(contentsStr, "\n")

messages := []string{"Enter Credentials:"}
origin := Center

asciiLines := []string{}
asciiStartLine := -1

for i, line := range contentsLines {
if strings.HasPrefix(line, "messages:") {
after, _ := strings.CutPrefix(line, "messages:")
messages = strings.Split(after, ",")
fields := strings.Fields(line)
if len(fields) >= 2 && fields[0] == "messages" && fields[1] == "=" {
if len(fields) < 3 {
return nil, errors.New("Formatting error in file: " + filename)
}
messages = strings.Split(fields[2], ",")
for i, message := range messages {
messages[i] = strings.TrimSpace(message)
}
}
if strings.HasPrefix(line, "origin") {
origin = Center
}
if strings.HasPrefix(line, "-") && asciiStartLine == -1 {
if strings.HasPrefix(line, "---") && asciiStartLine == -1 {
asciiStartLine = i
}
}
Expand All @@ -151,7 +141,6 @@ func parseAsciiFile(filename string) (*AsciiArt, error) {
strings.TrimSuffix(filename, "."+constants.AsciiFileExt),
strings.Join(asciiLines, "\n"),
messages,
origin,
)

return &ascii, nil
Expand All @@ -166,7 +155,7 @@ func loadLastSession() (*LastSession, error) {

contents := strings.Split(string(file), "\n")
if len(contents) < 2 {
return nil, errors.New("Last session file was configured incorrectly")
return nil, errors.New("Last session file was configured incorrectly.\nRun the command `# rm /etc/aporia/.last-session` to fix.")
}

return &LastSession{
Expand All @@ -181,11 +170,11 @@ func SaveSession(sessionName string, user string) {
os.WriteFile(constants.LastSessionFile, []byte(contents), 0644)
}

func parseConfigFile() *string {
func parseConfigFile() (*string, error) {
filepath := filepath.Join(constants.ConfigDir, constants.ConfigFile)
data, err := os.ReadFile(filepath)
if err != nil {
return nil
return nil, nil
}

contentsStr := string(data)
Expand All @@ -199,10 +188,13 @@ func parseConfigFile() *string {

fields := strings.Fields(line)
if fields[0] == "ascii" {
return &fields[2]
if len(fields) <= 2 {
return nil, errors.New("Error in user config file. Missing ascii art.")
}
return &fields[2], nil
}
}
return nil
return nil, nil
}

func LoadConfig() (*Config, error) {
Expand All @@ -217,12 +209,14 @@ func LoadConfig() (*Config, error) {
wg := sync.WaitGroup{}

asciiArts := []AsciiArt{}
asciiArtErrors := []error{}
parseEntry := func(entry fs.DirEntry) {
defer wg.Done()
filepath := filepath.Join(constants.ConfigDir, entry.Name())
if strings.HasSuffix(entry.Name(), "."+constants.AsciiFileExt) {
asciiFile, err := parseAsciiFile(entry.Name())
if err != nil {
asciiArtErrors = append(asciiArtErrors, err)
return
}
asciiArts = append(asciiArts, *asciiFile)
Expand All @@ -246,11 +240,29 @@ func LoadConfig() (*Config, error) {

session, _ := loadLastSession()

ascii, asciiError := parseConfigFile()
if asciiError != nil {
asciiArtErrors = append(asciiArtErrors, asciiError)
}

isAsciiError := false
if len(asciiArtErrors) != 0 {
isAsciiError = true
asciiArt := ""
for _, err := range asciiArtErrors {
asciiArt = asciiArt + fmt.Sprintln(err)
}
asciiArts = []AsciiArt{
newAsciiArt("Error", asciiArt, []string{"There was an error :("}),
}
}

return &Config{
AsciiArts: asciiArts,
Sessions: append(sessions, newShellSession()),
LastSession: session,
ascii: parseConfigFile(),
AsciiArts: asciiArts,
isAsciiError: isAsciiError,
Sessions: append(sessions, newShellSession()),
LastSession: session,
ascii: ascii,
}, nil
}

Expand All @@ -263,26 +275,29 @@ func DefaultConfig() Config {
}

func (self *Config) GetAscii() AsciiArt {
emptyAsciiArt := func() AsciiArt {
return newAsciiArt(
"This doesn't matter because it is never read.",
constants.DefaultAsciiArt,
constants.DefaultMessages(),
Center,
)
if self.isAsciiError {
return self.AsciiArts[0]
}

if self.ascii != nil {
for _, file := range self.AsciiArts {
if file.name == *self.ascii {
return file
}
}
os.Exit(0)
return emptyAsciiArt()
return newAsciiArt(
"This doesn't matter because it is never read.",
"ascii art `"+*self.ascii+"` not found",
constants.DefaultMessages(),
)
}

if len(self.AsciiArts) == 0 {
return emptyAsciiArt()
return newAsciiArt(
"This doesn't matter because it is never read.",
constants.DefaultAsciiArt,
constants.DefaultMessages(),
)
}

return self.AsciiArts[rand.Intn(len(self.AsciiArts))]
Expand Down
3 changes: 0 additions & 3 deletions config/desktop_crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"aporia/constants"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -51,8 +50,6 @@ func desktopCrawlPath(path string, sessionType SessionType) []Session {
lClean := strings.TrimSpace(parts[0])
rClean := strings.TrimSpace(parts[1])

fmt.Println(lClean)

if lClean == "Name" {
name = &rClean
}
Expand Down
2 changes: 1 addition & 1 deletion constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var ShutdownCommand = []string{"/bin/bash", "-c", "shutdown now"}
var RebootCommand = []string{"/bin/bash", "-c", "reboot"}

// Ascii art used when there is no config
const DefaultAsciiArt = ``
const DefaultAsciiArt = `Check the docs on how to add an ascii art!`

func DefaultMessages() []string {
return []string{"Login:"}
Expand Down
3 changes: 0 additions & 3 deletions examples/ffxiv.ascii
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
messages: G'raha Tia
origin: center
-----------------
7^
!5G~
~7~^:^~?5GGP:
Expand Down
3 changes: 0 additions & 3 deletions examples/luna.ascii
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
messages: Login
origin: center
-----------------
....
:YPB#####BBGP5YJ7~:.
.~7?YPG#&@@@@@@@@@&#GY?~.
Expand Down
4 changes: 1 addition & 3 deletions examples/sata_andagi.ascii
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
messages: SATA ANDAGI
origin: center

messages: SATA ANDAGI!, Sata Andagi
-------------
clllc'. .',,,,,;,,:,............''..'',,;:ccccc'..........';:llcll:;cllllllllllllllllllll:;;;;;;;;;;;,'..........','''''''................;;;;,;;;;,.............................................,:::cccccccccc
0000x,............................................':looooooldxl'.........';clllllodxkOOOOx;........';cdk0000OxkO0000000000000000000OOxdooooooolllc;'........:dxoolllllc;............'lxxdooololc,...........................................:dOkkOOOOOOOOO
Expand Down

0 comments on commit 67a6f6b

Please sign in to comment.