mirror of
https://github.com/ssh-vault/ssh-vault.git
synced 2025-04-19 07:42:18 +03:00
prototype
This commit is contained in:
parent
df2562b066
commit
c46dfa363d
27
LICENSE
Normal file
27
LICENSE
Normal file
@ -0,0 +1,27 @@
|
||||
Copyright (c) 2016, Nicolas Embriz
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of ssh-vault nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
24
Makefile
Normal file
24
Makefile
Normal file
@ -0,0 +1,24 @@
|
||||
.PHONY: all get test clean build cover
|
||||
|
||||
GO ?= go
|
||||
BIN_NAME=ssh-vault
|
||||
VERSION=$(shell git describe --tags --always)
|
||||
|
||||
all: clean build
|
||||
|
||||
get:
|
||||
${GO} get
|
||||
|
||||
build: get
|
||||
${GO} build -ldflags "-X main.version=${VERSION}" -o ${BIN_NAME} cmd/ssh-vault/main.go;
|
||||
|
||||
clean:
|
||||
@rm -rf ${BIN_NAME} ${BIN_NAME}.debug *.out build debian
|
||||
|
||||
test: get
|
||||
${GO} test -race -v
|
||||
|
||||
cover:
|
||||
${GO} test -cover && \
|
||||
${GO} test -coverprofile=coverage.out && \
|
||||
${GO} tool cover -html=coverage.out
|
32
cmd/ssh-vault/main.go
Normal file
32
cmd/ssh-vault/main.go
Normal file
@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
sv "github.com/ssh-vault/ssh-vault"
|
||||
)
|
||||
|
||||
var version string
|
||||
|
||||
func main() {
|
||||
parser := &sv.Parse{}
|
||||
|
||||
// flag set
|
||||
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||||
fs.Usage = parser.Usage(fs)
|
||||
|
||||
err := sv.ParseArgs(parser, fs)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// if -v print version
|
||||
if (fs.Lookup("v")).Value.(flag.Getter).Get().(bool) {
|
||||
fmt.Printf("%s\n", version)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
}
|
11
flags.go
Normal file
11
flags.go
Normal file
@ -0,0 +1,11 @@
|
||||
package sshvault
|
||||
|
||||
// Flags available command flags
|
||||
type Flags struct {
|
||||
Version bool
|
||||
Create string
|
||||
Decrypt string
|
||||
Edit string
|
||||
Encrypt string
|
||||
View string
|
||||
}
|
88
parser.go
Normal file
88
parser.go
Normal file
@ -0,0 +1,88 @@
|
||||
package sshvault
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Parser interface
|
||||
type Parser interface {
|
||||
Parse(fs *flag.FlagSet) (*Flags, error)
|
||||
isDir(path string) bool
|
||||
isFile(path string) bool
|
||||
}
|
||||
|
||||
// Parse implements parser
|
||||
type Parse struct {
|
||||
Flags
|
||||
}
|
||||
|
||||
// Parse parse the command line flags
|
||||
func (p *Parse) Parse(fs *flag.FlagSet) (*Flags, error) {
|
||||
fs.BoolVar(&p.Flags.Version, "v", false, "Print version")
|
||||
|
||||
err := fs.Parse(os.Args[1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &p.Flags, nil
|
||||
}
|
||||
func (p *Parse) Usage(fs *flag.FlagSet) func() {
|
||||
return func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s [create|decrypt|edit|encrypt|view] [-k key] [-u user] vault\n\n", os.Args[0])
|
||||
var flags []string
|
||||
fs.VisitAll(func(f *flag.Flag) {
|
||||
flags = append(flags, f.Name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Parse) isDir(path string) bool {
|
||||
f, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if m := f.Mode(); m.IsDir() && m&400 != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *Parse) isFile(path string) bool {
|
||||
f, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if m := f.Mode(); !m.IsDir() && m.IsRegular() && m&400 != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *Parse) checkWrkdir(dir string) (err error) {
|
||||
if !p.isDir(dir) {
|
||||
err = fmt.Errorf("-d %q does not exist or has wrong permissions, use (\"%s -h\") for help.", dir, os.Args[0])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ParseArgs parse command arguments
|
||||
func ParseArgs(p Parser, fs *flag.FlagSet) (err error) {
|
||||
flags, err := p.Parse(fs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// if -v
|
||||
if flags.Version {
|
||||
return
|
||||
}
|
||||
|
||||
// if no args
|
||||
if len(fs.Args()) < 1 {
|
||||
err = fmt.Errorf("Missing user, use (\"%s -h\") for help.", os.Args[0])
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user