Skip to content

Commit

Permalink
feat: add all services logger (i.e) remote command's functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
theiskaa committed Dec 6, 2022
1 parent a7c0d20 commit f1d36ee
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 32 deletions.
36 changes: 35 additions & 1 deletion lib/commands/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
package commands

import (
"github.com/fatih/color"
"github.com/insolite-dev/notya/lib/services"
"github.com/insolite-dev/notya/pkg"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -45,7 +48,19 @@ func initRemoteCommand() {
func runRemoteCommand(cmd *cobra.Command, args []string) {
determineService()

// TODO: list all active remote services.
loading.Start()
enabled, disabled := listAllRemote()
loading.Stop()

if len(enabled) > 0 {
pkg.Print("\nConnected Services:", color.FgGreen)
pkg.PrintServices(pkg.NOCOLOR, enabled)
}

if len(disabled) > 0 {
pkg.Print("\nUnreachable Services:", color.FgYellow)
pkg.PrintServices(pkg.NOCOLOR, disabled)
}
}

// runRemoteConnectCommand connects to a new remote service connection.
Expand All @@ -62,3 +77,22 @@ func runRemoteDisconnectCommand(cmd *cobra.Command, args []string) {

// TODO: add functionality to disconnect from remote service.
}

// Returns a list of all remote services by splitting them by their enabled or disabled level.
// first returned array includes "enabled" remote services, and second returned array includes "disabled" remote services.
func listAllRemote() ([]string, []string) {
allEnabled, allDisabled := []string{}, []string{}

for _, s := range services.RemoteServices {
switch s {
case services.FIRE.ToStr():
if services.IsFirebaseEnabled(service.StateConfig(), &localService) {
allEnabled = append(allEnabled, s)
} else {
allDisabled = append(allDisabled, s)
}
}
}

return allEnabled, allDisabled
}
5 changes: 0 additions & 5 deletions lib/models/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,3 @@ func (s *Settings) FirePath() string {
func (s *Settings) IsValid() bool {
return len(s.Name) > 0 && len(s.Editor) > 0 && len(s.NotesPath) > 0
}

// isFirebaseEnabled checks the validness of firebase fields.
func (s *Settings) IsFirebaseEnabled() bool {
return len(s.FirebaseProjectID) > 0 || len(s.FirebaseAccountKey) > 0 || len(s.FirebaseCollection) > 0
}
24 changes: 0 additions & 24 deletions lib/models/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,27 +175,3 @@ func TestIsValid(t *testing.T) {
})
}
}

func TestIsFirebaseEnabled(t *testing.T) {
tests := []struct {
settings models.Settings
expected bool
}{
{
settings: models.InitSettings("/usr/mock/NotesPath"),
expected: false,
},
{
settings: models.Settings{FirebaseProjectID: "mock-project-id"},
expected: true,
},
}

for _, td := range tests {
got := td.settings.IsFirebaseEnabled()

if got != td.expected {
t.Errorf("IsFirebaseEnabled sum was different: Want: %v | Got: %v", got, td.expected)
}
}
}
18 changes: 17 additions & 1 deletion lib/services/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@

package services

import "github.com/insolite-dev/notya/lib/models"
import (
"os"

"github.com/insolite-dev/notya/lib/models"
)

var (
LOCAL ServiceType = "LOCAL"
FIRE ServiceType = "FIREBASE"

// All services into one list: including local and remote.
Services []string = []string{
LOCAL.ToStr(),
FIRE.ToStr(),
}

// Only remote services into one list.
RemoteServices []string = []string{FIRE.ToStr()}
)

// Custom string struct to define type of services
Expand All @@ -33,6 +41,14 @@ func (s *ServiceType) ToStr() string {
return "undefined"
}

// IsFirebaseEnabled checks if firebase connection is enabled or not.
func IsFirebaseEnabled(s models.Settings, local *ServiceRepo) bool {
stargs := models.StdArgs{Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr}
err := NewFirebaseService(stargs, *local).Init()

return err == nil
}

// ServiceRepo is a abstract class for all service implementations.
// ╭──────╮ ╭────────────────────╮
// ... │ User │ ──▶ │ Interface Commands │
Expand Down
10 changes: 9 additions & 1 deletion pkg/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func PrintSettings(settings models.Settings) {
func PrintErrors(act string, errs []error) {
for i, e := range errs {
err := fmt.Sprintf("%v | %v",
fmt.Sprintf("%s%s%s", RED, fmt.Sprintf("- SWW fetch:%v", i+1), NOCOLOR),
fmt.Sprintf("%s%s%s", RED, fmt.Sprintf("- SWW %s:%v", act, i+1), NOCOLOR),
e.Error(),
)

Expand All @@ -181,3 +181,11 @@ func Spinner() *spinner.Spinner {
s.Color("yellow")
return s
}

// PrintServices logs given service names by provided color level.
func PrintServices(c string, services []string) {
for _, s := range services {
printable := fmt.Sprintf(" • %s", fmt.Sprintf("%s%s%s", c, s, NOCOLOR))
text.Println(printable)
}
}

0 comments on commit f1d36ee

Please sign in to comment.