mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: Add "coder" CLI (#221)
* feat: Add "coder" CLI * Add CLI test for login * Add "bin/coder" target to Makefile * Update promptui to fix race * Fix error scope * Don't run CLI tests on Windows * Fix requested changes
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Root represents the configuration directory.
|
||||
type Root string
|
||||
|
||||
func (r Root) Session() File {
|
||||
return File(filepath.Join(string(r), "session"))
|
||||
}
|
||||
|
||||
func (r Root) URL() File {
|
||||
return File(filepath.Join(string(r), "url"))
|
||||
}
|
||||
|
||||
func (r Root) Organization() File {
|
||||
return File(filepath.Join(string(r), "organization"))
|
||||
}
|
||||
|
||||
// File provides convenience methods for interacting with *os.File.
|
||||
type File string
|
||||
|
||||
// Delete deletes the file.
|
||||
func (f File) Delete() error {
|
||||
return os.Remove(string(f))
|
||||
}
|
||||
|
||||
// Write writes the string to the file.
|
||||
func (f File) Write(s string) error {
|
||||
return write(string(f), 0600, []byte(s))
|
||||
}
|
||||
|
||||
// Read reads the file to a string.
|
||||
func (f File) Read() (string, error) {
|
||||
byt, err := read(string(f))
|
||||
return string(byt), err
|
||||
}
|
||||
|
||||
// open opens a file in the configuration directory,
|
||||
// creating all intermediate directories.
|
||||
func open(path string, flag int, mode os.FileMode) (*os.File, error) {
|
||||
err := os.MkdirAll(filepath.Dir(path), 0750)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return os.OpenFile(path, flag, mode)
|
||||
}
|
||||
|
||||
func write(path string, mode os.FileMode, dat []byte) error {
|
||||
fi, err := open(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, mode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fi.Close()
|
||||
_, err = fi.Write(dat)
|
||||
return err
|
||||
}
|
||||
|
||||
func read(path string) ([]byte, error) {
|
||||
fi, err := open(path, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer fi.Close()
|
||||
return ioutil.ReadAll(fi)
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/cli/config"
|
||||
)
|
||||
|
||||
func TestFile(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("Write", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := config.Root(t.TempDir()).Session().Write("test")
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Read", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
root := config.Root(t.TempDir())
|
||||
err := root.Session().Write("test")
|
||||
require.NoError(t, err)
|
||||
data, err := root.Session().Read()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "test", data)
|
||||
})
|
||||
|
||||
t.Run("Delete", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
root := config.Root(t.TempDir())
|
||||
err := root.Session().Write("test")
|
||||
require.NoError(t, err)
|
||||
err = root.Session().Delete()
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user