Skip to content

Commit

Permalink
feat: given support for homeless shelter nix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
luisnquin committed Sep 2, 2023
1 parent 482a556 commit c0f45f1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
25 changes: 19 additions & 6 deletions cmd/nao/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"io"
"os"
"os/user"
Expand All @@ -16,6 +17,14 @@ import (
"github.com/rs/zerolog"
)

const DEFAULT_VERSION = "unversioned"

var (
version = DEFAULT_VERSION
commit string
date string
)

func main() {
defer func() {
if v := recover(); v != nil {
Expand Down Expand Up @@ -63,19 +72,23 @@ func main() {

logger.Trace().Msg("loading data...")

data, err := data.Load(&logger, config)
appData, err := data.Load(&logger, config)
if err != nil {
logger.Err(err).Msg("an error was encountered while loading data...")

ui.Error(err.Error())
os.Exit(1)
if errors.Is(err, data.ErrRunningOnHomelessShelter) {
logger.Warn().Err(err).Msg("error will be ignored")
ui.Warnf("got this error %s but it will be ignored", err.Error())
} else {
logger.Err(err).Msg("an error was encountered while loading data...")
ui.Error(err.Error())
os.Exit(1)
}
}

logger.Trace().Msg("executing command...")

ctx := context.Background()

if err := cmd.Execute(ctx, &logger, config, data); err != nil {
if err := cmd.Execute(ctx, &logger, config, appData); err != nil {
logger.Err(err).Msg("an error was encountered while executing command...")

ui.Error(err.Error())
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ func (c ModCmd) logKeyInUse(key string) (remove func() error, err error) {
}

func (c *ModCmd) getEditorName() string {
// return "kibi"

if c.editor != "" {
return c.editor
}
Expand Down
12 changes: 12 additions & 0 deletions internal/data/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package data

import (
"errors"
"strings"
)

var ErrRunningOnHomelessShelter = errors.New("running on homeless shelter")

func isHomelessShelterError(err error) bool {
return strings.Contains(err.Error(), "homeless-shelter")
}
7 changes: 5 additions & 2 deletions internal/data/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package data

import (
"fmt"
"io/ioutil"
"os"

"github.com/goccy/go-json"
Expand All @@ -16,7 +15,7 @@ func (b *Buffer) save() error {
return fmt.Errorf("unexpected error, can't format data buffer to json: %w", err)
}

return ioutil.WriteFile(b.config.FS.GetDataFile(), data, internal.PermReadWrite)
return os.WriteFile(b.config.FS.GetDataFile(), data, internal.PermReadWrite)
}

// First data load, if there's no file to load then it creates it.
Expand All @@ -27,6 +26,10 @@ func (b *Buffer) loadData() error {

err = os.MkdirAll(b.config.FS.GetDataDir(), os.ModePerm)
if err != nil {
if isHomelessShelterError(err) { // NixOS
return ErrRunningOnHomelessShelter
}

return fmt.Errorf("unable to create a new directory in '%s': %w", dataFile, err)
}

Expand Down
8 changes: 8 additions & 0 deletions internal/ui/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ func Fatal(message string) Suggest {
func Fatalf(message string, more ...any) Suggest {
return Fatal(fmt.Sprintf(message, more...))
}

func Warn(message string) {
fmt.Fprintf(os.Stderr, color.HEX("#fafa91").Sprintf("Warn: %s\n", message))
}

func Warnf(message string, more ...any) {
Warn(fmt.Sprintf(message, more...))
}

0 comments on commit c0f45f1

Please sign in to comment.