Skip to content

Commit

Permalink
feat:support quick login without flags
Browse files Browse the repository at this point in the history
  • Loading branch information
vimiix committed Dec 14, 2023
1 parent b2d049f commit 75f2ecd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cmd/ssx/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ssx [-i ENTRY_ID] [-s [USER@]HOST[:PORT]] [-k IDENTITY_FILE] [-t TAG_NAME]`,
SilenceErrors: true,
DisableAutoGenTag: true,
DisableSuggestions: true,
Args: cobra.ArbitraryArgs, // accept arbitrary args for supporting quick login
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
lg.SetVerbose(logVerbose)
if !printVersion {
Expand All @@ -44,6 +45,10 @@ ssx [-i ENTRY_ID] [-s [USER@]HOST[:PORT]] [-k IDENTITY_FILE] [-t TAG_NAME]`,
fmt.Fprintln(os.Stdout, version.Detail())
return nil
}
if len(args) > 0 {
// just use first word as search key
opt.Keyword = args[0]
}
return ssxInst.Main(cmd.Context())
},
}
Expand Down
53 changes: 51 additions & 2 deletions ssx/ssx.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type CmdOption struct {
Addr string
Tag string
IdentityFile string
Keyword string
}

// Tidy complete unset fields with default values
Expand Down Expand Up @@ -142,7 +143,9 @@ func (s *SSX) Main(ctx context.Context) error {
e *entry.Entry
err error
)
if s.opt.EntryID > 0 {
if s.opt.Keyword != "" {
e, err = s.searchEntry(s.opt.Keyword)
} else if s.opt.EntryID > 0 {
e, err = s.repo.GetEntry(s.opt.EntryID)
} else if s.opt.Addr != "" {
e, err = s.parseFuzzyAddr(s.opt.Addr)
Expand All @@ -158,7 +161,7 @@ func (s *SSX) Main(ctx context.Context) error {
return NewClient(e, s.repo).Run(ctx)
}

func (s *SSX) selectEntryFromAll() (*entry.Entry, error) {
func (s *SSX) getAllEntries() ([]*entry.Entry, error) {
var es []*entry.Entry
em, err := s.repo.GetAllEntries()
if err != nil {
Expand All @@ -172,6 +175,52 @@ func (s *SSX) selectEntryFromAll() (*entry.Entry, error) {
es = append(es, e)
}
}
return es, nil
}

// search by host and tag first, if not found, then connect as a new entry
func (s *SSX) searchEntry(keyword string) (*entry.Entry, error) {
es, err := s.getAllEntries()
if err != nil {
return nil, err
}
var candidates []*entry.Entry
for _, e := range es {
if strings.Contains(e.Host, keyword) ||
strings.Contains(strings.Join(e.Tags, " "), keyword) {
candidates = append(candidates, e)
}
}
if len(candidates) == 1 {
return candidates[0], nil
}
if len(candidates) > 1 {
return s.selectEntry(candidates)
}
lg.Debug("not found by keyword %q, treat it as new entry", keyword)
matches := addrRegex.FindStringSubmatch(keyword)
if len(matches) == 0 {
return nil, errors.Errorf("invalid address: %s", keyword)
}
username, host, port := matches[1], matches[2], matches[3]
e := &entry.Entry{
Host: host,
User: username,
Port: port,
KeyPath: s.opt.IdentityFile,
Source: entry.SourceSSXStore,
}
if err = e.Tidy(); err != nil {
return nil, err
}
return e, nil
}

func (s *SSX) selectEntryFromAll() (*entry.Entry, error) {
es, err := s.getAllEntries()
if err != nil {
return nil, err
}
return s.selectEntry(es)
}

Expand Down

0 comments on commit 75f2ecd

Please sign in to comment.