Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expand search to included files #6

Merged
merged 9 commits into from
Sep 24, 2024
2 changes: 1 addition & 1 deletion cmd/ggh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Main() {
action, value := command.Which()
switch action {
case command.InteractiveHistory:
args = history.Interactive()
args = interactive.History()
case command.InteractiveConfig:
args = interactive.Config("")
case command.InteractiveConfigWithSearch:
Expand Down
9 changes: 7 additions & 2 deletions internal/config/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import (
"path/filepath"
)

func GetSshDir() string {
func HomeDir() string {
userHomeDir, err := os.UserHomeDir()

if err != nil {
return ""
}

return filepath.Join(userHomeDir, ".ssh")
return userHomeDir
}

func GetSshDir() string {

return filepath.Join(HomeDir(), ".ssh")
}

func GetConfigFile() string {
Expand Down
57 changes: 39 additions & 18 deletions internal/config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ func ParseWithSearch(search string, configFile string) ([]SSHConfig, error) {
continue
}

if search != "" && !strings.Contains(lines[0], search) {
continue
}

sshConfig := SSHConfig{
Name: lines[0],
Port: "",
Expand All @@ -58,7 +54,7 @@ func ParseWithSearch(search string, configFile string) ([]SSHConfig, error) {
}
switch {
case strings.Contains(line, "Include"):
result, err := ParseInclude(value)
result, err := ParseInclude(search, value)
if err != nil {
panic(err)
}
Expand All @@ -74,36 +70,61 @@ func ParseWithSearch(search string, configFile string) ([]SSHConfig, error) {
}
}

if sshConfig.Host != "" {
configs = append(configs, sshConfig)
if sshConfig.Host == "" || !strings.Contains(sshConfig.Name, search) {
continue
}

configs = append(configs, sshConfig)

}

return configs, nil
}

func ParseInclude(path string) ([]SSHConfig, error) {
func ParseInclude(search string, path string) ([]SSHConfig, error) {
var results = make([]SSHConfig, 0)

if filepath.IsLocal(path) {
var isAbsolute = path[0] == '/' || path[0] == '~'

var paths []string
var err error

if isAbsolute {
if path[0] == '~' {
path = filepath.Join(HomeDir(), path[2:])
}
} else {
path = filepath.Join(GetSshDir(), path)
}

info, err := os.Stat(path)
if err != nil || info.IsDir() {
return results, err
}
paths, err = filepath.Glob(path)

fileContent, err := os.ReadFile(path)
if err != nil {
return nil, err
}

items, err := Parse(string(fileContent))
if err != nil {
return nil, err
for _, path := range paths {
info, err := os.Stat(path)

if err != nil {
return nil, err
}

if info.IsDir() {
continue
}

fileContent, err := os.ReadFile(path)
if err != nil {
return nil, err
}

items, err := ParseWithSearch(search, string(fileContent))
if err != nil {
return nil, err
}
results = append(results, items...)
}
results = append(results, items...)

return results, nil
}
Expand Down
37 changes: 11 additions & 26 deletions internal/history/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import (
"encoding/json"
"fmt"
"github.com/byawitz/ggh/internal/config"
"github.com/byawitz/ggh/internal/interactive"
"github.com/byawitz/ggh/internal/ssh"
"github.com/byawitz/ggh/internal/theme"
"github.com/charmbracelet/bubbles/table"
"log"
"os"
"time"
)

Expand All @@ -34,33 +31,21 @@ func Fetch(file []byte) ([]SSHHistory, error) {
return nil, err
}

return historyList, nil
}
func Interactive() []string {
list, err := FetchWithDefaultFile()
search, err := config.ParseWithSearch("", config.GetConfigFile())

if err != nil {
log.Fatal(err)
return historyList, nil
}

if len(list) == 0 {
fmt.Println("No history found.")
os.Exit(0)
for i, history := range historyList {
for _, sshConfig := range search {
if sshConfig.Host == history.Connection.Host {
historyList[i].Connection.Name = sshConfig.Name
}
}
}

var rows []table.Row
currentTime := time.Now()
for _, history := range list {
rows = append(rows, table.Row{
history.Connection.Host,
history.Connection.Port,
history.Connection.User,
history.Connection.Key,
fmt.Sprintf("%s", readableTime(currentTime.Sub(history.Date))),
})
}
c := interactive.Select(rows, interactive.SelectHistory)
return ssh.GenerateCommandArgs(c)
return historyList, nil
}

func Print() {
Expand All @@ -82,14 +67,14 @@ func Print() {
history.Connection.Port,
history.Connection.User,
history.Connection.Key,
fmt.Sprintf("%s", readableTime(currentTime.Sub(history.Date))),
fmt.Sprintf("%s", ReadableTime(currentTime.Sub(history.Date))),
})
}

fmt.Println(theme.PrintTable(rows, theme.PrintHistory))
}

func readableTime(d time.Duration) string {
func ReadableTime(d time.Duration) string {
if d.Seconds() < 60 {
return fmt.Sprintf("%d seconds ago", int(d.Seconds()))
}
Expand Down
34 changes: 32 additions & 2 deletions internal/history/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/byawitz/ggh/internal/config"
"github.com/charmbracelet/bubbles/table"
"os"
"slices"
"strings"
Expand All @@ -14,7 +15,6 @@ func AddHistoryFromArgs(args []string) {
if len(args) == 1 && !strings.Contains(args[0], "@") {
localConfig, err := config.GetConfig(args[0])
if err != nil || localConfig.Name == "" {
fmt.Printf("couldn't fetch %s from config file, error:%v.\n", args[0], err)
return
}

Expand Down Expand Up @@ -70,6 +70,33 @@ func AddHistory(c config.SSHConfig) {
}
}

func RemoveByIP(row table.Row) {
list, err := Fetch(getFile())

if err != nil {
fmt.Println("error getting ggh file")
return
}

ip := row[1]

saving := make([]SSHHistory, 0, len(list)-1)

for _, item := range list {
if item.Connection.Host == ip {
continue
}

saving = append(saving, item)
}

err = saveFile(SSHHistory{}, saving)
if err != nil {
panic("error saving ggh file")
}

}

func saveFile(n SSHHistory, l []SSHHistory) error {
file := getFileLocation()
fileContent := stringify(n, l)
Expand All @@ -89,7 +116,10 @@ func stringify(n SSHHistory, l []SSHHistory) string {
}
}

history = append(history, n)
if n.Connection.Host != "" {
history = append(history, n)
}

history = append(history, l...)
content, err := json.Marshal(history)

Expand Down
31 changes: 31 additions & 0 deletions internal/interactive/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package interactive
import (
"fmt"
"github.com/byawitz/ggh/internal/config"
"github.com/byawitz/ggh/internal/history"
"github.com/byawitz/ggh/internal/ssh"
"github.com/charmbracelet/bubbles/table"
"log"
"os"
"time"
)

func Config(value string) []string {
Expand All @@ -28,3 +31,31 @@ func Config(value string) []string {
c := Select(rows, SelectConfig)
return ssh.GenerateCommandArgs(c)
}

func History() []string {
list, err := history.FetchWithDefaultFile()

if err != nil {
log.Fatal(err)
}

if len(list) == 0 {
fmt.Println("No history found.")
os.Exit(0)
}

var rows []table.Row
currentTime := time.Now()
for _, historyItem := range list {
rows = append(rows, table.Row{
historyItem.Connection.Name,
historyItem.Connection.Host,
historyItem.Connection.Port,
historyItem.Connection.User,
historyItem.Connection.Key,
fmt.Sprintf("%s", history.ReadableTime(currentTime.Sub(historyItem.Date))),
})
}
c := Select(rows, SelectHistory)
return ssh.GenerateCommandArgs(c)
}
Loading