Skip to content

Commit

Permalink
add list ooss
Browse files Browse the repository at this point in the history
  • Loading branch information
AdheipSingh committed Dec 19, 2024
1 parent db5208c commit 4e23367
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 5 deletions.
86 changes: 86 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cmd

// Copyright (c) 2024 Parseable, Inc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import (
"fmt"
"log"
"os"
"path/filepath"
"pb/pkg/installer"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

// ListOssCmd lists the Parseable OSS servers
var ListOssCmd = &cobra.Command{
Use: "oss",
Short: "List available Parseable OSS servers",
Example: "pb list oss",
Run: func(cmd *cobra.Command, _ []string) {
// Read the installer file

Check failure on line 36 in cmd/list.go

View workflow job for this annotation

GitHub Actions / Build and Test the Go code

unused-parameter: parameter 'cmd' seems to be unused, consider removing or renaming it as _ (revive)
entries, err := readInstallerFile()
if err != nil {
log.Fatalf("Failed to list OSS servers: %v", err)
}

// Check if there are no entries
if len(entries) == 0 {
fmt.Println("No OSS servers found.")
return
}

// Display the entries in a table format
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Namespace", "Version", "Status"})

for _, entry := range entries {
table.Append([]string{entry.Name, entry.Namespace, entry.Version, entry.Status})
}

table.Render()
},
}

// readInstallerFile reads and parses the installer.yaml file
func readInstallerFile() ([]installer.InstallerEntry, error) {
// Define the file path
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to get user home directory: %w", err)
}
filePath := filepath.Join(homeDir, ".parseable", "installer.yaml")

// Check if the file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil, fmt.Errorf("installer file not found at %s", filePath)
}

// Read and parse the file
data, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to read installer file: %w", err)
}

var entries []installer.InstallerEntry
if err := yaml.Unmarshal(data, &entries); err != nil {
return nil, fmt.Errorf("failed to parse installer file: %w", err)
}

return entries, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/gofrs/flock v0.12.1
github.com/manifoldco/promptui v0.9.0
github.com/oklog/ulid/v2 v2.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1
github.com/spf13/pflag v1.0.5
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
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.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand Down Expand Up @@ -342,6 +343,8 @@ github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo/v2 v2.21.0 h1:7rg/4f3rB88pb5obDgNZrNHrQ4e6WpjonchcpuBRnZM=
github.com/onsi/ginkgo/v2 v2.21.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
github.com/onsi/gomega v1.35.1 h1:Cwbd75ZBPxFSuZ6T+rN/WCb/gOc6YgFBXLlZLhC7Ds4=
Expand Down
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ var install = &cobra.Command{
},
}

var list = &cobra.Command{
Use: "list",
Short: "List parseable on kubernetes cluster",
Long: "\nlist command is used to list parseable oss installations.",
PersistentPreRunE: combinedPreRun,
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if os.Getenv("PB_ANALYTICS") == "disable" {
return
}
wg.Add(1)
go func() {
defer wg.Done()
analytics.PostRunAnalytics(cmd, "install", args)
}()
},
}

var uninstall = &cobra.Command{
Use: "uninstall",
Short: "Uninstall parseable on kubernetes cluster",
Expand Down Expand Up @@ -247,6 +264,7 @@ func main() {
schema.AddCommand(pb.CreateSchemaCmd)

install.AddCommand(pb.InstallOssCmd)
list.AddCommand(pb.ListOssCmd)

uninstall.AddCommand(pb.UnInstallOssCmd)

Expand All @@ -261,6 +279,7 @@ func main() {
cli.AddCommand(install)
cli.AddCommand(uninstall)
cli.AddCommand(schema)
cli.AddCommand(list)

// Set as command
pb.VersionCmd.Run = func(_ *cobra.Command, _ []string) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func waterFall(verbose bool) {
log.Fatalf("Failed to deploy parseable, err: %v", err)
}

if err := updateInstallerFile(installerEntry{
if err := updateInstallerFile(InstallerEntry{
Name: pbInfo.Name,
Namespace: pbInfo.Namespace,
Version: config.Version,
Expand Down Expand Up @@ -828,16 +828,16 @@ func openBrowser(url string) {
cmd.Start()
}

// installerEntry represents an entry in the installer.yaml file
type installerEntry struct {
// InstallerEntry represents an entry in the installer.yaml file

Check failure on line 831 in pkg/installer/installer.go

View workflow job for this annotation

GitHub Actions / Build and Test the Go code

exported: type name will be used as installer.InstallerEntry by other packages, and that stutters; consider calling this Entry (revive)
type InstallerEntry struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
Version string `yaml:"version"`
Status string `yaml:"status"` // todo ideally should be a heartbeat
}

// updateInstallerFile updates or creates the installer.yaml file with deployment info
func updateInstallerFile(entry installerEntry) error {
func updateInstallerFile(entry InstallerEntry) error {
// Define the file path
homeDir, err := os.UserHomeDir()
if err != nil {
Expand All @@ -851,7 +851,7 @@ func updateInstallerFile(entry installerEntry) error {
}

// Read existing entries if the file exists
var entries []installerEntry
var entries []InstallerEntry
if _, err := os.Stat(filePath); err == nil {
// File exists, load existing content
data, err := os.ReadFile(filePath)
Expand Down

0 comments on commit 4e23367

Please sign in to comment.