Commit e0f51b85 authored by Kirill Unitsaev's avatar Kirill Unitsaev

Merge branch 'devel' into 'master'

add json format See merge request !2
parents 661d0a49 aa008de8
package config
import "github.com/urfave/cli/v3"
const (
FormatText = "text"
FormatJSON = "json"
)
var FormatFlag = &cli.StringFlag{
Name: "format",
Usage: "output format (text, json)",
Value: FormatText,
Aliases: []string{"o"},
}
func IsJSON(cmd *cli.Command) bool {
return cmd.String("format") == FormatJSON
}
...@@ -168,6 +168,9 @@ func HyprlandInfoModulesCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -168,6 +168,9 @@ func HyprlandInfoModulesCommand(ctx context.Context, cmd *cli.Command) error {
modules := manager.GetModulesList(user, filter) modules := manager.GetModulesList(user, filter)
if len(modules) == 0 { if len(modules) == 0 {
if config.IsJSON(cmd) {
return ui.PrintJSON([]ui.JSONItem{})
}
return fmt.Errorf("нет доступных модулей") return fmt.Errorf("нет доступных модулей")
} }
...@@ -191,6 +194,10 @@ func HyprlandInfoModulesCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -191,6 +194,10 @@ func HyprlandInfoModulesCommand(ctx context.Context, cmd *cli.Command) error {
}) })
} }
if config.IsJSON(cmd) {
return ui.PrintJSON(ui.TreeItemsToJSON(items))
}
ui.RenderTree(ui.RenderTreeOptions{ ui.RenderTree(ui.RenderTreeOptions{
Title: "Modules", Title: "Modules",
Items: items, Items: items,
...@@ -235,3 +242,59 @@ func HyprlandModuleEditCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -235,3 +242,59 @@ func HyprlandModuleEditCommand(ctx context.Context, cmd *cli.Command) error {
return nil return nil
} }
func HyprlandModuleShowCommand(ctx context.Context, cmd *cli.Command) error {
module := cmd.Args().Get(0)
if module == "" {
return fmt.Errorf("укажите имя модуля")
}
user := cmd.Bool("user")
manager, err := GetHyprlandManager(ctx)
if err != nil {
return err
}
info := manager.GetModuleInfo(module, user)
if !info.Available {
return fmt.Errorf("недопустимое название для модуля")
}
errors, _ := manager.CheckModule(module, user)
if config.IsJSON(cmd) {
jsonErrors := make([]ModuleErrorJSON, len(errors))
for i, e := range errors {
jsonErrors[i] = ModuleErrorJSON{Line: e.Line, Text: e.Text}
}
return ui.PrintJSON(ModuleShowJSON{
Name: info.Name,
Status: info.Status.Label,
Path: info.Path,
ConfPath: info.ConfPath,
LineNumber: info.LineNumber,
Available: info.Available,
Errors: jsonErrors,
})
}
fmt.Printf("Модуль: %s\n", info.Name)
fmt.Printf("Статус: %s\n", info.Status.Color("%s %s", info.Status.Symbol, info.Status.Label))
fmt.Printf("Путь: %s\n", info.Path)
fmt.Printf("Путь в конфиге: %s\n", info.ConfPath)
fmt.Printf("Строка в конфиге: %d\n", info.LineNumber)
if info.Available {
fmt.Println("Доступен: да")
} else {
fmt.Println("Доступен: нет")
}
if len(errors) > 0 {
fmt.Printf("\nОшибки (%d):\n", len(errors))
for _, e := range errors {
fmt.Printf(" %d: %s\n", e.Line, e.Text)
}
}
return nil
}
...@@ -83,6 +83,16 @@ func CommandList() *cli.Command { ...@@ -83,6 +83,16 @@ func CommandList() *cli.Command {
ShellComplete: ShellCompleteModule("all"), ShellComplete: ShellCompleteModule("all"),
}, },
{ {
Name: "show",
Usage: "Show detailed module info",
ArgsUsage: "module",
Flags: []cli.Flag{
config.FormatFlag,
},
Action: HyprlandModuleShowCommand,
ShellComplete: ShellCompleteModule("all"),
},
{
Name: "info", Name: "info",
Usage: "Information about modules", Usage: "Information about modules",
Flags: []cli.Flag{ Flags: []cli.Flag{
...@@ -92,6 +102,7 @@ func CommandList() *cli.Command { ...@@ -92,6 +102,7 @@ func CommandList() *cli.Command {
Aliases: []string{"f"}, Aliases: []string{"f"},
Value: "all", Value: "all",
}, },
config.FormatFlag,
}, },
Action: HyprlandInfoModulesCommand, Action: HyprlandInfoModulesCommand,
}, },
...@@ -131,13 +142,19 @@ func CommandList() *cli.Command { ...@@ -131,13 +142,19 @@ func CommandList() *cli.Command {
Usage: "Hyprland vars", Usage: "Hyprland vars",
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
Name: "list", Name: "list",
Usage: "vars list", Usage: "vars list",
Flags: []cli.Flag{
config.FormatFlag,
},
Action: HyprlandVarListCommand, Action: HyprlandVarListCommand,
}, },
{ {
Name: "info", Name: "info",
Usage: "vars info", Usage: "vars info",
Flags: []cli.Flag{
config.FormatFlag,
},
Action: HyprlandVarInfoCommand, Action: HyprlandVarInfoCommand,
}, },
{ {
...@@ -164,8 +181,11 @@ func CommandList() *cli.Command { ...@@ -164,8 +181,11 @@ func CommandList() *cli.Command {
Usage: "Hyprland plugins", Usage: "Hyprland plugins",
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
Name: "list", Name: "list",
Usage: "Hyprland plugins list", Usage: "Hyprland plugins list",
Flags: []cli.Flag{
config.FormatFlag,
},
Action: HyprlandPluginListCommand, Action: HyprlandPluginListCommand,
}, },
{ {
...@@ -185,6 +205,7 @@ func CommandList() *cli.Command { ...@@ -185,6 +205,7 @@ func CommandList() *cli.Command {
Aliases: []string{"f"}, Aliases: []string{"f"},
Value: "all", Value: "all",
}, },
config.FormatFlag,
}, },
Action: HyprlandPluginInfoCommand, Action: HyprlandPluginInfoCommand,
}, },
......
package hyprland
type ModuleShowJSON struct {
Name string `json:"name"`
Status string `json:"status"`
Path string `json:"path"`
ConfPath string `json:"conf_path"`
LineNumber int `json:"line_number"`
Available bool `json:"available"`
Errors []ModuleErrorJSON `json:"errors"`
}
type ModuleErrorJSON struct {
Line int `json:"line"`
Text string `json:"text"`
}
...@@ -17,6 +17,15 @@ func HyprlandPluginListCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -17,6 +17,15 @@ func HyprlandPluginListCommand(ctx context.Context, cmd *cli.Command) error {
return err return err
} }
list := manager.GetPluginsList(cmd.String("filter")) list := manager.GetPluginsList(cmd.String("filter"))
if config.IsJSON(cmd) {
items := make([]ui.JSONItem, len(list))
for i, name := range list {
items[i] = ui.JSONItem{Name: name}
}
return ui.PrintJSON(items)
}
fmt.Println(strings.Join(list, "\n")) fmt.Println(strings.Join(list, "\n"))
return nil return nil
} }
...@@ -37,6 +46,9 @@ func HyprlandPluginInfoCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -37,6 +46,9 @@ func HyprlandPluginInfoCommand(ctx context.Context, cmd *cli.Command) error {
} }
plugins := manager.GetPluginsList(cmd.String("filter")) plugins := manager.GetPluginsList(cmd.String("filter"))
if len(plugins) == 0 { if len(plugins) == 0 {
if config.IsJSON(cmd) {
return ui.PrintJSON([]ui.JSONItem{})
}
return fmt.Errorf("нет доступных плагинов") return fmt.Errorf("нет доступных плагинов")
} }
...@@ -59,6 +71,10 @@ func HyprlandPluginInfoCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -59,6 +71,10 @@ func HyprlandPluginInfoCommand(ctx context.Context, cmd *cli.Command) error {
}) })
} }
if config.IsJSON(cmd) {
return ui.PrintJSON(ui.TreeItemsToJSON(items))
}
ui.RenderTree(ui.RenderTreeOptions{ ui.RenderTree(ui.RenderTreeOptions{
Title: "Plugins", Title: "Plugins",
Items: items, Items: items,
......
...@@ -21,6 +21,14 @@ func HyprlandVarListCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -21,6 +21,14 @@ func HyprlandVarListCommand(ctx context.Context, cmd *cli.Command) error {
return err return err
} }
if config.IsJSON(cmd) {
items := make([]ui.JSONItem, len(manager.Vars))
for i, v := range manager.Vars {
items[i] = ui.JSONItem{Name: v.Name, Value: v.Value}
}
return ui.PrintJSON(items)
}
for _, v := range manager.Vars { for _, v := range manager.Vars {
fmt.Println(v.Name) fmt.Println(v.Name)
} }
...@@ -37,6 +45,9 @@ func HyprlandVarInfoCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -37,6 +45,9 @@ func HyprlandVarInfoCommand(ctx context.Context, cmd *cli.Command) error {
info := manager.GetVarList() info := manager.GetVarList()
if len(info) == 0 { if len(info) == 0 {
if config.IsJSON(cmd) {
return ui.PrintJSON([]ui.JSONItem{})
}
color.Yellow("Нет переменных в конфигурации") color.Yellow("Нет переменных в конфигурации")
return nil return nil
} }
...@@ -53,6 +64,14 @@ func HyprlandVarInfoCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -53,6 +64,14 @@ func HyprlandVarInfoCommand(ctx context.Context, cmd *cli.Command) error {
}) })
} }
if config.IsJSON(cmd) {
jsonItems := make([]ui.JSONItem, len(info))
for i, v := range info {
jsonItems[i] = ui.JSONItem{Name: v.Name, Value: v.Value}
}
return ui.PrintJSON(jsonItems)
}
ui.RenderTree(ui.RenderTreeOptions{ ui.RenderTree(ui.RenderTreeOptions{
Title: "Vars", Title: "Vars",
Items: items, Items: items,
......
...@@ -396,10 +396,14 @@ func createProfile(profileName string, prof config.PresetProfile, dryRun bool, r ...@@ -396,10 +396,14 @@ func createProfile(profileName string, prof config.PresetProfile, dryRun bool, r
processProfile(prof, dryRun, res) processProfile(prof, dryRun, res)
} }
func ShowProfilesInfo() { func ShowProfilesInfo(jsonFormat bool) {
profiles := config.Env.Preset.Profiles profiles := config.Env.Preset.Profiles
if len(profiles) == 0 { if len(profiles) == 0 {
if jsonFormat {
ui.PrintJSON([]ui.JSONItem{})
return
}
color.Red("Нет доступных профилей") color.Red("Нет доступных профилей")
return return
} }
...@@ -416,6 +420,11 @@ func ShowProfilesInfo() { ...@@ -416,6 +420,11 @@ func ShowProfilesInfo() {
}) })
} }
if jsonFormat {
ui.PrintJSON(ui.TreeItemsToJSON(items))
return
}
ui.RenderTree(ui.RenderTreeOptions{ ui.RenderTree(ui.RenderTreeOptions{
Title: "Profiles", Title: "Profiles",
Items: items, Items: items,
...@@ -429,7 +438,7 @@ func presetApplyCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -429,7 +438,7 @@ func presetApplyCommand(ctx context.Context, cmd *cli.Command) error {
profileName := cmd.Args().Get(0) profileName := cmd.Args().Get(0)
if profileName == "" { if profileName == "" {
ShowProfilesInfo() ShowProfilesInfo(false)
return nil return nil
} }
...@@ -475,6 +484,6 @@ func presetApplyAllCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -475,6 +484,6 @@ func presetApplyAllCommand(ctx context.Context, cmd *cli.Command) error {
} }
func presetInfoCommand(ctx context.Context, cmd *cli.Command) error { func presetInfoCommand(ctx context.Context, cmd *cli.Command) error {
ShowProfilesInfo() ShowProfilesInfo(config.IsJSON(cmd))
return nil return nil
} }
...@@ -14,8 +14,11 @@ func CommandList() *cli.Command { ...@@ -14,8 +14,11 @@ func CommandList() *cli.Command {
Usage: "Manage preset configuration profiles", Usage: "Manage preset configuration profiles",
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
Name: "info", Name: "info",
Usage: "Show information about a preset profiles", Usage: "Show information about a preset profiles",
Flags: []cli.Flag{
config.FormatFlag,
},
Action: presetInfoCommand, Action: presetInfoCommand,
}, },
{ {
......
package repo package repo
import ( import (
"ximperconf/config"
"context" "context"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"os" "os"
"regexp" "regexp"
"ximperconf/config"
"ximperconf/ui"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
) )
type DeferredInfo struct {
LastUpdate string `json:"last_update"`
ArchiveDate string `json:"archive_date"`
}
var ( var (
reLastUpdate = regexp.MustCompile(`Last update date: ([0-9:\- ]+)`) reLastUpdate = regexp.MustCompile(`Last update date: ([0-9:\- ]+)`)
reArchiveDate = regexp.MustCompile(`Sisyphus archive date: ([0-9\-]+)`) reArchiveDate = regexp.MustCompile(`Sisyphus archive date: ([0-9\-]+)`)
...@@ -73,6 +78,13 @@ func DeferredInfoCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -73,6 +78,13 @@ func DeferredInfoCommand(ctx context.Context, cmd *cli.Command) error {
lastUpdate := deferredLastUpdate(html) lastUpdate := deferredLastUpdate(html)
archiveDate := deferredArchiveDate(html) archiveDate := deferredArchiveDate(html)
if config.IsJSON(cmd) {
return ui.PrintJSON(DeferredInfo{
LastUpdate: lastUpdate,
ArchiveDate: archiveDate,
})
}
color.Green("Deferred:") color.Green("Deferred:")
fmt.Printf(" Последнее обновление: %s\n", lastUpdate) fmt.Printf(" Последнее обновление: %s\n", lastUpdate)
fmt.Printf(" Дата архива: %s\n", archiveDate) fmt.Printf(" Дата архива: %s\n", archiveDate)
......
package repo package repo
import "github.com/urfave/cli/v3" import (
"ximperconf/config"
"github.com/urfave/cli/v3"
)
func CommandList() *cli.Command { func CommandList() *cli.Command {
return &cli.Command{ return &cli.Command{
...@@ -12,8 +16,11 @@ func CommandList() *cli.Command { ...@@ -12,8 +16,11 @@ func CommandList() *cli.Command {
Usage: "Deferred repo", Usage: "Deferred repo",
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
Name: "info", Name: "info",
Usage: "Deferred repo info", Usage: "Deferred repo info",
Flags: []cli.Flag{
config.FormatFlag,
},
Action: DeferredInfoCommand, Action: DeferredInfoCommand,
}, },
{ {
......
package ui
import (
"encoding/json"
"fmt"
)
type JSONItem struct {
Name string `json:"name"`
Status string `json:"status,omitempty"`
Description string `json:"description,omitempty"`
Value string `json:"value,omitempty"`
}
func TreeItemsToJSON(items []TreeItem) []JSONItem {
result := make([]JSONItem, len(items))
for i, item := range items {
result[i] = JSONItem{
Name: item.Name,
Status: item.Status.Label,
Description: item.Description,
}
}
return result
}
func PrintJSON(data interface{}) error {
output, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment