Fix assignee filter bug in List
This commit is contained in:
165
service/fileconfig.go
Normal file
165
service/fileconfig.go
Normal file
@@ -0,0 +1,165 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type fileConfig struct {
|
||||
path string
|
||||
User string `json:"user"`
|
||||
UserAliases []*Alias `json:"aliases"`
|
||||
}
|
||||
|
||||
var defaultAliases = []*Alias{
|
||||
{Name: "mine", Command: "list --assignee $me --tag _status::open", Description: "Show open tasks assigned to you"},
|
||||
{Name: "due", Command: "list --tag _status::open --tag _due", Description: "Show open tasks with due dates"},
|
||||
{Name: "new", Command: "create $@", Description: "Create a new task"},
|
||||
}
|
||||
|
||||
func LoadConfig() (Config, error) {
|
||||
path, err := findConfigPath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return loadConfig(path)
|
||||
}
|
||||
|
||||
func loadConfig(path string) (*fileConfig, error) {
|
||||
fc := &fileConfig{path: path, UserAliases: []*Alias{}}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if err := json.Unmarshal(data, fc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
func findConfigPath() (string, error) {
|
||||
dir, err := filepath.Abs(".")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for {
|
||||
p := filepath.Join(dir, ".axconfig")
|
||||
if _, err := os.Stat(p); err == nil {
|
||||
return p, nil
|
||||
}
|
||||
if parent := filepath.Dir(dir); parent == dir {
|
||||
break
|
||||
} else {
|
||||
dir = parent
|
||||
}
|
||||
}
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(home, ".config", "ax", "config.json"), nil
|
||||
}
|
||||
|
||||
func (c *fileConfig) GetUser() string {
|
||||
if c.User != "" {
|
||||
return c.User
|
||||
}
|
||||
if u := os.Getenv("AX_USER"); u != "" {
|
||||
return u
|
||||
}
|
||||
if u, err := user.Current(); err == nil {
|
||||
return u.Username
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func (c *fileConfig) SetUser(username string) error {
|
||||
c.User = username
|
||||
return c.Save()
|
||||
}
|
||||
|
||||
func (c *fileConfig) GetAlias(name string) (*Alias, error) {
|
||||
for _, a := range c.UserAliases {
|
||||
if a.Name == name {
|
||||
return a, nil
|
||||
}
|
||||
}
|
||||
for _, a := range defaultAliases {
|
||||
if a.Name == name {
|
||||
return a, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("alias not found")
|
||||
}
|
||||
|
||||
func (c *fileConfig) SetAlias(alias *Alias) error {
|
||||
for i, a := range c.UserAliases {
|
||||
if a.Name == alias.Name {
|
||||
c.UserAliases[i] = alias
|
||||
return c.Save()
|
||||
}
|
||||
}
|
||||
c.UserAliases = append(c.UserAliases, alias)
|
||||
return c.Save()
|
||||
}
|
||||
|
||||
func (c *fileConfig) DeleteAlias(name string) error {
|
||||
for i, a := range c.UserAliases {
|
||||
if a.Name == name {
|
||||
c.UserAliases = slices.Delete(c.UserAliases, i, i+1)
|
||||
return c.Save()
|
||||
}
|
||||
}
|
||||
for _, a := range defaultAliases {
|
||||
if a.Name == name {
|
||||
return errors.New("cannot delete default alias")
|
||||
}
|
||||
}
|
||||
return errors.New("alias not found")
|
||||
}
|
||||
|
||||
func (c *fileConfig) ListAliases() ([]*Alias, error) {
|
||||
seen := make(map[string]bool)
|
||||
var result []*Alias
|
||||
for _, a := range c.UserAliases {
|
||||
result = append(result, a)
|
||||
seen[a.Name] = true
|
||||
}
|
||||
for _, a := range defaultAliases {
|
||||
if !seen[a.Name] {
|
||||
result = append(result, a)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (c *fileConfig) Save() error {
|
||||
if err := os.MkdirAll(filepath.Dir(c.path), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.MarshalIndent(c, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(c.path, data, 0644)
|
||||
}
|
||||
|
||||
func ExpandAlias(alias *Alias, args []string, currentUser string) []string {
|
||||
cmd := alias.Command
|
||||
cmd = strings.ReplaceAll(cmd, "$me", currentUser)
|
||||
if len(args) > 0 {
|
||||
cmd = strings.ReplaceAll(cmd, "$@", strings.Join(args, " "))
|
||||
}
|
||||
for i, arg := range args {
|
||||
cmd = strings.ReplaceAll(cmd, "$"+string(rune('1'+i)), arg)
|
||||
}
|
||||
return strings.Fields(cmd)
|
||||
}
|
||||
Reference in New Issue
Block a user