Skip to content

Commit

Permalink
V0.1.0 (#20)
Browse files Browse the repository at this point in the history
* Updates README.md

Signed-off-by: Saahil Bhavsar <saahil_bhavsar@outlook.com>

* Separates selector for unix and windows

Signed-off-by: Saahil Bhavsar <saahil_bhavsar@outlook.com>

* Updates dependencies

Signed-off-by: Saahil Bhavsar <saahil_bhavsar@outlook.com>

---------

Signed-off-by: Saahil Bhavsar <saahil_bhavsar@outlook.com>
  • Loading branch information
SaahilNotSahil authored Jul 25, 2024
1 parent 913d2f1 commit b545b36
Show file tree
Hide file tree
Showing 78 changed files with 9,535 additions and 4,271 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ An easier way for remote server authentication. Powered by [OpenPubkey](https://
- **Scalability**: Add as many people as necessary to your server via their emails.
- **Security**: This project integrates [OpenPubkey](https://github.com/openpubkey/openpubkey), leveraging the OpenID Connect (OIDC) Protocol for enhanced SSH authentication security.
- **Single Command**: Configure your server for SPoK with just one command.
- **Server Aliases**: Add aliases for your servers for easy access.
- **Runs Everywhere**: Set up SPoK on any machine—whether it's local, remote, cloud-based, physical server, or a VM—and on any architecture, including x86 or ARM

## Why SPok?
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/zitadel/oidc/v2 v2.11.0
golang.org/x/crypto v0.21.0
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
golang.org/x/sys v0.22.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -37,7 +38,6 @@ require (
github.com/segmentio/asm v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.33.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !windows
// +build !windows

package selector

import (
Expand Down Expand Up @@ -81,7 +84,9 @@ func (m *Menu) Display() string {
return ""
} else if keyCode == enter {
menuItem := m.MenuItems[m.CursorPos]

fmt.Println("\r")

return menuItem.ID
} else if keyCode == up {
m.CursorPos = (m.CursorPos + len(m.MenuItems) - 1) % len(m.MenuItems)
Expand Down Expand Up @@ -109,9 +114,8 @@ func getInput() byte {
log.Fatal(err)
}

var read int
readBytes := make([]byte, 3)
read, err = t.Read(readBytes)
read, err := t.Read(readBytes)

t.Restore()
t.Close()
Expand Down
153 changes: 153 additions & 0 deletions internal/pkg/selector/selector_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
//go:build windows
// +build windows

package selector

import (
"fmt"
"log"
"os"

"github.com/buger/goterm"
"golang.org/x/sys/windows"
)

type Menu struct {
Prompt string
CursorPos int
MenuItems []*MenuItem
}

type MenuItem struct {
Text string
ID string
}

var up byte = 65
var down byte = 66
var escape byte = 27
var enter byte = 13
var ctrl_c byte = 3

var keys = map[byte]bool{
up: true,
down: true,
}

func (m *Menu) AddItem(option string, id string) *Menu {
menuItem := &MenuItem{
Text: option,
ID: id,
}

m.MenuItems = append(m.MenuItems, menuItem)

return m
}

func (m *Menu) renderMenuItems(redraw bool) {
if redraw {
fmt.Printf("\033[%dA", len(m.MenuItems)-1)
}

for index, menuItem := range m.MenuItems {
var newline = "\n"

if index == len(m.MenuItems)-1 {
newline = ""
}

menuItemText := menuItem.Text
cursor := " "
if index == m.CursorPos {
cursor = goterm.Color("> ", goterm.YELLOW)
menuItemText = goterm.Color(menuItemText, goterm.YELLOW)
}

fmt.Printf("\r%s %s%s", cursor, menuItemText, newline)
}
}

func (m *Menu) Display() string {
defer func() {
fmt.Printf("\033[?25h")
}()

fmt.Printf("%s\n", goterm.Color(goterm.Bold(m.Prompt)+":", goterm.CYAN))

m.renderMenuItems(false)

fmt.Printf("\033[?25l")

for {
keyCode := getInput()
if keyCode == escape {
return ""
} else if keyCode == enter {
menuItem := m.MenuItems[m.CursorPos]

fmt.Println("\r")

return menuItem.ID
} else if keyCode == up {
m.CursorPos = (m.CursorPos + len(m.MenuItems) - 1) % len(m.MenuItems)
m.renderMenuItems(true)
} else if keyCode == down {
m.CursorPos = (m.CursorPos + 1) % len(m.MenuItems)
m.renderMenuItems(true)
} else if keyCode == ctrl_c {
return ""
}
}
}

func (m *Menu) Clear() {
for i := 0; i < (len(m.MenuItems) + 1); i++ {
fmt.Print("\033[F\033[K")
}
}

func getInput() byte {
hConsole := windows.Handle(os.Stdin.Fd())

var originalMode uint32

err := windows.GetConsoleMode(hConsole, &originalMode)
if err != nil {
log.Fatal(err)
}

rawMode := originalMode &^ (windows.ENABLE_ECHO_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_INPUT)

err = windows.SetConsoleMode(hConsole, rawMode)
if err != nil {
log.Fatal(err)
}

defer func() {
windows.SetConsoleMode(hConsole, originalMode)
}()

readBytes := make([]byte, 3)
read, err := os.Stdin.Read(readBytes)
if err != nil {
log.Fatal(err)
}

if read == 3 {
if _, ok := keys[readBytes[2]]; ok {
return readBytes[2]
}
} else {
return readBytes[0]
}

return 0
}

func NewMenu(prompt string) *Menu {
return &Menu{
Prompt: prompt,
MenuItems: make([]*MenuItem, 0),
}
}
1 change: 1 addition & 0 deletions vendor/golang.org/x/sys/cpu/cpu.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/golang.org/x/sys/cpu/cpu_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/golang.org/x/sys/cpu/cpu_arm64.s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/golang.org/x/sys/cpu/cpu_gc_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/golang.org/x/sys/cpu/cpu_linux_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b545b36

Please sign in to comment.