first commit

This commit is contained in:
i.smyshlyaev
2025-10-16 12:46:23 +03:00
commit 9c813d4754
37 changed files with 1671 additions and 0 deletions

BIN
cli/awesome_cli Executable file

Binary file not shown.

29
cli/cmd/cli.go Normal file
View File

@@ -0,0 +1,29 @@
package cmd
import (
"os"
"github.com/spf13/cobra"
)
var (
RootCmd = &cobra.Command{
Use: "acli",
Short: "Интерфейс командной строки",
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
},
}
)
func Execute() {
if err := RootCmd.Execute(); err != nil {
panic(err)
}
}

26
cli/cmd/list.go Normal file
View File

@@ -0,0 +1,26 @@
package cmd
import (
"awesome_cli/internal/service"
"awesome_cli/internal/views"
"github.com/spf13/cobra"
)
func init() {
RootCmd.AddCommand(list())
}
func list() *cobra.Command {
c := &cobra.Command{
Use: "list",
Aliases: []string{"l"},
Short: "Вывод списка",
Run: func(cmd *cobra.Command, args []string) {
vms := service.VMService().List()
views.List(vms)
},
}
return c
}

42
cli/go.mod Normal file
View File

@@ -0,0 +1,42 @@
module awesome_cli
go 1.25
require (
github.com/charmbracelet/huh v0.7.0
github.com/charmbracelet/lipgloss v1.1.0
github.com/gofrs/uuid v4.4.0+incompatible
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.10.1
golang.org/x/term v0.36.0
)
require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/catppuccin/go v0.3.0 // indirect
github.com/charmbracelet/bubbles v0.21.0 // indirect
github.com/charmbracelet/bubbletea v1.3.4 // indirect
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect
github.com/charmbracelet/x/ansi v0.8.0 // indirect
github.com/charmbracelet/x/cellbuf v0.0.13 // indirect
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.16.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.37.0 // indirect
golang.org/x/text v0.28.0 // indirect
)

102
cli/go.sum Normal file
View File

@@ -0,0 +1,102 @@
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8=
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
github.com/catppuccin/go v0.3.0 h1:d+0/YicIq+hSTo5oPuRi5kOpqkVA5tAsU6dNhvRu+aY=
github.com/catppuccin/go v0.3.0/go.mod h1:8IHJuMGaUUjQM82qBrGNBv7LFq6JI3NnQCF6MOlZjpc=
github.com/charmbracelet/bubbles v0.21.0 h1:9TdC97SdRVg/1aaXNVWfFH3nnLAwOXr8Fn6u6mfQdFs=
github.com/charmbracelet/bubbles v0.21.0/go.mod h1:HF+v6QUR4HkEpz62dx7ym2xc71/KBHg+zKwJtMw+qtg=
github.com/charmbracelet/bubbletea v1.3.4 h1:kCg7B+jSCFPLYRA52SDZjr51kG/fMUEoPoZrkaDHyoI=
github.com/charmbracelet/bubbletea v1.3.4/go.mod h1:dtcUCyCGEX3g9tosuYiut3MXgY/Jsv9nKVdibKKRRXo=
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs=
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk=
github.com/charmbracelet/huh v0.7.0 h1:W8S1uyGETgj9Tuda3/JdVkc3x7DBLZYPZc4c+/rnRdc=
github.com/charmbracelet/huh v0.7.0/go.mod h1:UGC3DZHlgOKHvHC07a5vHag41zzhpPFj34U92sOmyuk=
github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY=
github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2llXn7xE=
github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q=
github.com/charmbracelet/x/cellbuf v0.0.13 h1:/KBBKHuVRbq1lYx5BzEHBAFBP8VcQzJejZ/IA3iR28k=
github.com/charmbracelet/x/cellbuf v0.0.13/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs=
github.com/charmbracelet/x/conpty v0.1.0 h1:4zc8KaIcbiL4mghEON8D72agYtSeIgq8FSThSPQIb+U=
github.com/charmbracelet/x/conpty v0.1.0/go.mod h1:rMFsDJoDwVmiYM10aD4bH2XiRgwI7NYJtQgl5yskjEQ=
github.com/charmbracelet/x/errors v0.0.0-20240508181413-e8d8b6e2de86 h1:JSt3B+U9iqk37QUU2Rvb6DSBYRLtWqFqfxf8l5hOZUA=
github.com/charmbracelet/x/errors v0.0.0-20240508181413-e8d8b6e2de86/go.mod h1:2P0UgXMEa6TsToMSuFqKFQR+fZTO9CNGUNokkPatT/0=
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payRxjMjKgx2PaCWLZ4p3ro9y97+TVLZNaRZgJwSVDQ=
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 h1:qko3AQ4gK1MTS/de7F5hPGx6/k1u0w4TeYmBFwzYVP4=
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0/go.mod h1:pBhA0ybfXv6hDjQUZ7hk1lVxBiUbupdw5R31yPUViVQ=
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
github.com/charmbracelet/x/termios v0.1.1 h1:o3Q2bT8eqzGnGPOYheoYS8eEleT5ZVNYNy8JawjaNZY=
github.com/charmbracelet/x/termios v0.1.1/go.mod h1:rB7fnv1TgOPOyyKRJ9o+AsTU/vK5WHJ2ivHeut/Pcwo=
github.com/charmbracelet/x/xpty v0.1.2 h1:Pqmu4TEJ8KeA9uSkISKMU3f+C1F6OGBn8ABuGlqCbtI=
github.com/charmbracelet/x/xpty v0.1.2/go.mod h1:XK2Z0id5rtLWcpeNiMYBccNNBrP2IJnzHI0Lq13Xzq4=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.36.0 h1:zMPR+aF8gfksFprF/Nc/rd1wRS1EI6nDBGyWAvDzx2Q=
golang.org/x/term v0.36.0/go.mod h1:Qu394IJq6V6dCBRgwqshf3mPF85AqzYEzofzRdZkWss=
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

42
cli/internal/entity/vm.go Normal file
View File

@@ -0,0 +1,42 @@
package entity
import (
"awesome_cli/internal/ui"
)
type VirtualMachineOutput struct {
UUID string `json:"vm_uuid,omitempty"`
Status VMStatus `json:"status,omitempty"`
Name string `json:"name,omitempty"`
Autostart bool `json:"autostart,omitempty"`
CPU int `json:"cpu,omitempty"`
DatastoreName string `json:"datastore_name,omitempty"`
Memory int `json:"memory,omitempty"`
}
type VMStatus int
const (
Creating VMStatus = iota + 1
Running
Stopped
Failed
Paused
)
func (s VMStatus) String() string {
switch s {
case Creating:
return ui.SetColor(ui.Yellow, "Creating")
case Running:
return ui.SetColor(ui.Green, "Running")
case Stopped:
return ui.SetColor(ui.Red, "Stopped")
case Failed:
return ui.SetColor(ui.Red, "Failed")
case Paused:
return ui.SetColor(ui.Yellow, "Paused")
default:
return "Unknown"
}
}

View File

@@ -0,0 +1,37 @@
package service
import (
"awesome_cli/internal/entity"
"github.com/gofrs/uuid"
)
type VM struct {
}
func VMService() *VM {
return &VM{}
}
func (vm *VM) List() []entity.VirtualMachineOutput {
u, _ := uuid.NewV4()
return []entity.VirtualMachineOutput{
{
UUID: u.String(),
Status: 1,
Name: "Awesome VM",
Autostart: false,
CPU: 8,
DatastoreName: "datastore",
Memory: 8,
},
{
UUID: u.String(),
Status: 2,
Name: "Awesome VM2",
Autostart: true,
CPU: 18,
DatastoreName: "datastore2",
Memory: 8,
},
}
}

View File

@@ -0,0 +1,166 @@
package ui
import (
"fmt"
"strconv"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/huh"
log "github.com/sirupsen/logrus"
)
type FormBuilder struct {
widgets []huh.Field
form huh.Form
}
func NewForm() *FormBuilder {
return &FormBuilder{}
}
func (f *FormBuilder) AddWidget(w huh.Field) {
f.widgets = append(f.widgets, w)
}
func (f *FormBuilder) Group(w ...huh.Field) *huh.Group {
group := huh.NewGroup(w...)
return group
}
func (f *FormBuilder) BuildGroups(g ...*huh.Group) *huh.Form {
form := huh.NewForm(g...)
return form
}
func (f *FormBuilder) Build() *huh.Form {
form := huh.NewForm(huh.NewGroup(f.widgets...)).WithTheme(ThemeCustom())
return form
}
func (f *FormBuilder) GetString(key string) string {
fmt.Println(f.form.GetString(key))
return f.form.GetString(key)
}
func (f *FormBuilder) GetBool(key string) bool {
fmt.Println(f.form.GetString(key))
return f.form.GetBool(key)
}
func (f *FormBuilder) GetInt(key string) int64 {
v := f.form.GetString(key)
fmt.Println(v)
value, err := strconv.Atoi(v)
if err != nil {
log.WithError(err).Fatalf(fmt.Sprintf("failed to convert value %s to integer", v))
return 0
}
return int64(value)
}
func MultiSelect(title, key string, options []string) *huh.MultiSelect[string] {
s := huh.NewMultiSelect[string]().
Title(title).
Options(huh.NewOptions[string](options...)...).
Key(key)
return s
}
func InteractiveList(title, key string, opts map[string]string) *huh.Select[string] {
var pick string
var options []huh.Option[string]
s := huh.NewSelect[string]().
Title(title).
Key(key).
Value(&pick)
if opts != nil {
for k, v := range opts {
options = append(options, huh.Option[string]{Key: k, Value: v})
}
s.Options(options...)
}
return s
}
func Input(question string, key string) *huh.Input {
var answer string
input := huh.NewInput().
Title(fmt.Sprintf("%s", question)).
Prompt("> ").
Validate(ValidateString).
Key(key).
Value(&answer)
return input
}
func Confirm(question string, key string) *huh.Confirm {
var happy bool
confirm := huh.NewConfirm().
Title(question).
Affirmative("Yes").
Negative("No").
Value(&happy).
Key(key)
return confirm
}
func Text(title, desc string) *huh.Note {
t := huh.NewNote().
Title(title).
Description(desc)
return t
}
func TextField(title, key string) *huh.Text {
t := huh.NewText().
Title(title).Key(key).ShowLineNumbers(true).Editor("vim")
return t
}
func ThemeCustom() *huh.Theme {
t := huh.ThemeBase()
var (
normalFg = lipgloss.AdaptiveColor{Light: "235", Dark: "252"}
// indigo = lipgloss.AdaptiveColor{Light: "#5A56E0", Dark: "#7571F9"}
indigo = lipgloss.AdaptiveColor{Light: "32", Dark: "33"}
cream = lipgloss.AdaptiveColor{Light: "#FFFDF5", Dark: "#FFFDF5"}
// fuchsia = lipgloss.Color("#F780E2")
green = lipgloss.AdaptiveColor{Light: "#02BA84", Dark: "#02BF87"}
red = lipgloss.AdaptiveColor{Light: "#FF4672", Dark: "#ED567A"}
)
t.Focused.Base = t.Focused.Base.BorderForeground(lipgloss.Color("238"))
t.Focused.Title = t.Focused.Title.Foreground(White).Bold(true)
t.Focused.NoteTitle = t.Focused.NoteTitle.Foreground(White).Bold(true).MarginBottom(1)
t.Focused.Directory = t.Focused.Directory.Foreground(indigo)
t.Focused.Description = t.Focused.Description.Foreground(lipgloss.AdaptiveColor{Light: "", Dark: "243"})
t.Focused.ErrorIndicator = t.Focused.ErrorIndicator.Foreground(red)
t.Focused.ErrorMessage = t.Focused.ErrorMessage.Foreground(red)
t.Focused.SelectSelector = t.Focused.SelectSelector.Foreground(green)
t.Focused.NextIndicator = t.Focused.NextIndicator.Foreground(indigo)
t.Focused.PrevIndicator = t.Focused.PrevIndicator.Foreground(indigo)
t.Focused.Option = t.Focused.Option.Foreground(normalFg)
t.Focused.MultiSelectSelector = t.Focused.MultiSelectSelector.Foreground(green)
t.Focused.SelectedOption = t.Focused.SelectedOption.Foreground(green)
t.Focused.SelectedPrefix = lipgloss.NewStyle().Foreground(green).SetString("✓ ")
t.Focused.UnselectedPrefix = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "", Dark: "243"}).SetString("• ")
t.Focused.UnselectedOption = t.Focused.UnselectedOption.Foreground(normalFg)
t.Focused.FocusedButton = t.Focused.FocusedButton.Foreground(cream).Background(indigo)
t.Focused.Next = t.Focused.FocusedButton
t.Focused.BlurredButton = t.Focused.BlurredButton.Foreground(normalFg).Background(lipgloss.AdaptiveColor{Light: "252", Dark: "237"})
t.Focused.TextInput.Cursor = t.Focused.TextInput.Cursor.Foreground(Gray)
t.Focused.TextInput.Placeholder = t.Focused.TextInput.Placeholder.Foreground(lipgloss.AdaptiveColor{Light: "248", Dark: "238"})
t.Focused.TextInput.Prompt = t.Focused.TextInput.Prompt.Foreground(green)
t.Blurred = t.Focused
t.Blurred.Base = t.Focused.Base.BorderStyle(lipgloss.HiddenBorder())
t.Blurred.NextIndicator = lipgloss.NewStyle()
t.Blurred.PrevIndicator = lipgloss.NewStyle()
return t
}

View File

@@ -0,0 +1,84 @@
package ui
import (
"fmt"
"strconv"
"github.com/charmbracelet/huh"
log "github.com/sirupsen/logrus"
)
const (
proceedTemplate = "%s (y/n): "
defaultTemplate = "%s: "
chooseTemplate = "%d : %s\n"
)
func AskToConfirm(question, affirmative, negative string) bool {
confirm := Confirm(question, "res")
confirm.Affirmative(affirmative)
confirm.Negative(negative)
form := huh.NewForm(huh.NewGroup(confirm))
form.Run()
if form.State != huh.StateCompleted {
log.Fatal("canceled")
}
return form.GetBool("res")
}
func AskToValue(question string) (string, error) {
input := Input(question, "res")
form := huh.NewForm(huh.NewGroup(input))
form.Run()
if form.State != huh.StateCompleted {
log.Fatal("canceled")
}
return form.GetString("res"), nil
}
func AskToInt(question string) (int, error) {
v, err := AskToValue(question)
if err != nil {
return 0, err
}
value, err := strconv.Atoi(v)
if err != nil {
return 0, err
}
return value, nil
}
func AskToChoose(question string, list map[int]string) string {
for i, item := range list {
fmt.Printf(chooseTemplate, i, item)
}
answer, err := AskToInt(question)
if err != nil {
log.WithError(err).Error("failed to read the answer")
}
return list[answer]
}
func AskToChooseInteractive(question string, list []string) string {
values := make(map[string]string)
if len(list) == 0 {
log.Fatal("no items found")
}
for _, item := range list {
values[item] = item
}
widget := InteractiveList(question, "res", values)
form := huh.NewForm(huh.NewGroup(widget)).WithShowHelp(false)
form.Run()
if form.State != huh.StateCompleted {
log.Fatal("canceled")
}
return form.GetString("res")
}

155
cli/internal/ui/render.go Normal file
View File

@@ -0,0 +1,155 @@
package ui
import (
"fmt"
"os"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
terminal "golang.org/x/term"
)
var (
// ANSI256 256 colors, 8-bit
Blue = lipgloss.Color("21")
Magenta = lipgloss.Color("13")
Gray = lipgloss.Color("238")
White = lipgloss.Color("15")
Red = lipgloss.Color("9")
Green = lipgloss.Color("10")
Yellow = lipgloss.Color("11")
// styling
headerStyle = lipgloss.NewStyle().
Background(Blue).
Foreground(White).
Padding(0, 1).
Italic(true)
infoStyle = headerStyle.Copy().
Foreground(White).
Background(Gray)
subHeaderStyle = headerStyle.Copy().
Background(lipgloss.NoColor{})
errorStyle = lipgloss.NewStyle().Foreground(Red)
)
type TableBuilder struct {
tableHeader []string
rows [][]string
Table *table.Table
padding int
}
func NewTableBuilder() *TableBuilder {
return &TableBuilder{
Table: table.New(),
padding: 1,
}
}
func (t *TableBuilder) Reset() {
t.tableHeader = nil
t.rows = nil
t.Table = table.New()
}
func (t *TableBuilder) AddTableHeader(strs ...string) {
t.tableHeader = append(t.tableHeader, strs...)
}
func (t *TableBuilder) AddRow(strs ...string) {
t.rows = append(t.rows, strs)
}
func (t *TableBuilder) Width(w int) {
t.Table.Width(w)
}
func (t *TableBuilder) colWidth(colNumber int) int {
var maxWidth int
for _, row := range t.rows {
for col, v := range row {
if col == colNumber && maxWidth < len(v)+t.padding*2 {
maxWidth = len(v) + t.padding*2
}
}
}
return maxWidth
}
func (t *TableBuilder) CreateTable() *table.Table {
width, _, _ := terminal.GetSize(0)
t.Table.
BorderRow(false).BorderColumn(false).
BorderLeft(false).BorderRight(false).BorderTop(false).BorderBottom(false).
//Border(lipgloss.RoundedBorder()).
//BorderRow(true).
//BorderStyle(BorderStyle).
StyleFunc(func(row, col int) lipgloss.Style {
re := lipgloss.NewRenderer(os.Stdout)
if row == -1 {
// Align(lipgloss.Center)
return re.NewStyle().Bold(true).Align(lipgloss.Left).Padding(0, 2, 0, 0).Foreground(Green)
}
return re.NewStyle().Padding(0, 2, 0, 0).Align(lipgloss.Left)
}).
Headers(t.tableHeader...).
Rows(t.rows...)
switch {
case width < 100:
t.Table.Width(width)
case len(t.tableHeader) > 8:
t.Table.Width(width)
}
return t.Table
}
// Justify Выравнивание по ширине терминала
func (t *TableBuilder) Justify() {
width, _, _ := terminal.GetSize(0)
t.Table.Width(width)
}
func Header(s string) string {
return headerStyle.Render(s)
}
func SubHeader(s string) string {
return subHeaderStyle.Render(s)
}
func Info(s string) string {
return infoStyle.Render(s)
}
func StringBar(strs ...string) string {
return lipgloss.JoinHorizontal(lipgloss.Left, strs...)
}
func ErrorBar(s string) string {
return errorStyle.Render(s)
}
func SetColor(color lipgloss.Color, str string) string {
return lipgloss.NewStyle().Foreground(color).Render(str)
}
func Spinner(stop chan bool, description string) {
spinner := []rune{'|', '\\', '-', '/'}
for {
for _, r := range spinner {
select {
case <-stop:
fmt.Printf("\r")
return
default:
fmt.Printf("\r%c %s", r, description)
time.Sleep(100 * time.Millisecond)
}
}
}
}

View File

@@ -0,0 +1,14 @@
package ui
import (
"errors"
"regexp"
)
func ValidateString(s string) error {
validRegex := regexp.MustCompile(`^[a-zA-Z0-9_.]+$`)
if !validRegex.MatchString(s) {
return errors.New("invalid input")
}
return nil
}

35
cli/internal/views/vm.go Normal file
View File

@@ -0,0 +1,35 @@
package views
import (
"fmt"
"strconv"
"awesome_cli/internal/entity"
"awesome_cli/internal/ui"
)
func List(vms []entity.VirtualMachineOutput) {
var runCount int
t := ui.NewTableBuilder()
t.AddTableHeader("Status", "Name", "vCPU", "Memory", "Datastore")
for _, vm := range vms {
t.AddRow(
vm.Status.String(),
vm.Name,
strconv.FormatInt(int64(vm.CPU), 10),
strconv.FormatInt(int64(vm.Memory), 10),
vm.DatastoreName,
)
if vm.Status == entity.Running {
runCount++
}
}
header := ui.Header("Virtual machines")
count := ui.Info(fmt.Sprintf("Total: %d", len(vms)))
running := ui.Info(fmt.Sprintf("Run: %d", runCount))
fmt.Printf("%s\n\n", ui.StringBar(header, count, running))
fmt.Println(t.CreateTable())
}

7
cli/main.go Normal file
View File

@@ -0,0 +1,7 @@
package main
import "awesome_cli/cmd"
func main() {
cmd.Execute()
}