|
| 1 | +package lister |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + "bytes" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/abdfnx/gosh" |
| 11 | + "github.com/spf13/viper" |
| 12 | + "github.com/briandowns/spinner" |
| 13 | + "github.com/abdfnx/bubbles/list" |
| 14 | + "github.com/charmbracelet/lipgloss" |
| 15 | + "github.com/scmn-dev/secman/constants" |
| 16 | + "github.com/scmn-dev/secman/internal/shared" |
| 17 | +) |
| 18 | + |
| 19 | +type Passwords struct { |
| 20 | + logins []list.Item |
| 21 | + ccs []list.Item |
| 22 | + emails []list.Item |
| 23 | + notes []list.Item |
| 24 | + servers []list.Item |
| 25 | +} |
| 26 | + |
| 27 | +func SPW() *Passwords { |
| 28 | + return &Passwords{ |
| 29 | + logins: readPasswords("-l"), |
| 30 | + ccs: readPasswords("-c"), |
| 31 | + emails: readPasswords("-e"), |
| 32 | + notes: readPasswords("-n"), |
| 33 | + servers: readPasswords("-s"), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func readPasswords(p string) []list.Item { |
| 38 | + if len(os.Args) > 1 && len(os.Args) != 3 { |
| 39 | + if strings.Contains(os.Args[1], "list") || strings.Contains(os.Args[1], ".") { |
| 40 | + s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) |
| 41 | + s.Suffix = " 📡 Preparing & Getting data..." |
| 42 | + s.Start() |
| 43 | + |
| 44 | + err, out, errout := gosh.RunOutput("sc . " + p) |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + fmt.Println(err) |
| 48 | + fmt.Println(errout) |
| 49 | + |
| 50 | + os.Exit(1) |
| 51 | + } |
| 52 | + |
| 53 | + s.Stop() |
| 54 | + |
| 55 | + if strings.Contains(out, "401") { |
| 56 | + st := shared.DefaultStyles() |
| 57 | + |
| 58 | + head := st.Error.Render("\n\nYour Authentication is Expired.") |
| 59 | + body := st.Subtle.Render(" Refresh your authentication via `secman auth refresh`.") |
| 60 | + |
| 61 | + fmt.Println(lipgloss.NewStyle().PaddingLeft(2).SetString(constants.Logo("Secman Lister") + st.Wrap.Render(head + body)).String()) |
| 62 | + |
| 63 | + os.Exit(0) |
| 64 | + } |
| 65 | + |
| 66 | + viper.SetConfigType("yaml") |
| 67 | + viper.ReadConfig(bytes.NewBuffer([]byte(out))) |
| 68 | + items := make([]list.Item, 0) |
| 69 | + |
| 70 | + for _, line := range strings.Split(viper.GetString("passwords"), "\n") { |
| 71 | + if line[0] == '#' || len(line) == 0 { |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + fields := strings.Split(line, "-|-") |
| 76 | + |
| 77 | + if p == "-l" { |
| 78 | + if len(fields) != 5 { |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + items = append(items, NewLoginListItem(strings.TrimSpace(fields[0]), strings.TrimSpace(fields[1]), strings.TrimSpace(fields[2]), strings.TrimSpace(fields[3]), strings.TrimSpace(fields[4]))) |
| 83 | + } else if p == "-c" { |
| 84 | + if len(fields) != 6 { |
| 85 | + continue |
| 86 | + } |
| 87 | + |
| 88 | + items = append(items, NewCCListItem(strings.TrimSpace(fields[0]), strings.TrimSpace(fields[1]), strings.TrimSpace(fields[2]), strings.TrimSpace(fields[3]), strings.TrimSpace(fields[4]), strings.TrimSpace(fields[5]))) |
| 89 | + } else if p == "-e" { |
| 90 | + if len(fields) != 3 { |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + items = append(items, NewEmailListItem(strings.TrimSpace(fields[0]), strings.TrimSpace(fields[1]), strings.TrimSpace(fields[2]))) |
| 95 | + } else if p == "-n" { |
| 96 | + items = append(items, NewNoteListItem(strings.TrimSpace(fields[0]), strings.TrimSpace(fields[1]), strings.TrimSpace(fields[2]))) |
| 97 | + } else if p == "-s" { |
| 98 | + if len(fields) != 10 { |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + items = append(items, NewServerListItem(strings.TrimSpace(fields[0]), strings.TrimSpace(fields[1]), strings.TrimSpace(fields[2]), strings.TrimSpace(fields[3]), strings.TrimSpace(fields[4]), strings.TrimSpace(fields[5]), strings.TrimSpace(fields[6]), strings.TrimSpace(fields[7]), strings.TrimSpace(fields[8]), strings.TrimSpace(fields[9]))) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return items |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +func (c *Passwords) PWs(p string) []list.Item { |
| 114 | + if p == "-l" { |
| 115 | + return c.logins |
| 116 | + } else if p == "-c" { |
| 117 | + return c.ccs |
| 118 | + } else if p == "-e" { |
| 119 | + return c.emails |
| 120 | + } else if p == "-n" { |
| 121 | + return c.notes |
| 122 | + } else if p == "-s" { |
| 123 | + return c.servers |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
0 commit comments