Skip to content

Commit

Permalink
Add oya init cli command.
Browse files Browse the repository at this point in the history
  • Loading branch information
bilus committed Nov 12, 2018
1 parent 77f96ae commit 254dd31
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 26 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2018 Marcin Bilski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
46 changes: 46 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright © 2018 Marcin Bilski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
"os"

i "github.com/bilus/oya/cmd/init"
"github.com/spf13/cobra"
)

// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize an Oya project",
RunE: func(cmd *cobra.Command, args []string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
return i.Init(cwd, os.Stdout, os.Stderr)
},
Args: cobra.NoArgs,
}

func init() {
rootCmd.AddCommand(initCmd)
}
95 changes: 95 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright © 2018 Marcin Bilski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
"fmt"
"os"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "oya",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.oya.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".oya" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".oya")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
8 changes: 8 additions & 0 deletions features/init.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ Scenario: Init a project
When I run "oya init"
Then the command succeeds
And file ./Oyafile exists

Scenario: Init a existing project
When I run "oya init"
And I run "oya init"
Then the command fails with error matching
"""
.*already an Oya project.*
"""
44 changes: 22 additions & 22 deletions oya.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package main

import (
"fmt"
"os"
// Copyright © 2018 Marcin Bilski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

kasia "github.com/ziutek/kasia.go"
)
package main

type Ctx struct {
H, W string
}
import "github.com/bilus/oya/cmd"

func main() {
ctx := map[string]string{"H": "Hello", "W": "world"}

tpl, err := kasia.Parse("$H $W $$O!\n")
if err != nil {
fmt.Println(err)
return
}

err = tpl.Run(os.Stdout, ctx)
if err != nil {
fmt.Println(err)
}
cmd.Execute()
}
4 changes: 4 additions & 0 deletions pkg/oyafile/oyafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func LoadFromDir(dirPath, rootDir string) (*Oyafile, bool, error) {
}

func InitDir(dirPath string) error {
_, found, err := LoadFromDir(dirPath, dirPath)
if err == nil && found {
return errors.Errorf("already an Oya project")
}
f, err := os.Create(fullPath(dirPath, ""))
if err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions todo.org
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
* Sharing Values
* DONE Sharing Values
CLOSED: [2018-11-12 Mon 14:10]
** Import via modules
/Oyafile
/oya.mod
Expand All @@ -8,15 +9,15 @@
main: github.com/Tooploox/pledge
** Same but Oyafile contains Module:
** Simple directory-level inheritance
* Add a tutorial on using docker package
* CLI
* Bring README up to date
* Simplify oya get/vendor (based on Import statements) TBD
** Just use Import
** oya get -- adds to packages.lock if not there
** oya records new imports using sha in packages.lock
** oya vendor -- vendors recorded imports (keep .git)
** oya get -u <uri> updates package sha in packages.lock and fetches it
* Bring README up to date
* Add a tutorial on using docker package
* CLI
* Install hook for packages
* Error if user adds a capitalized hook/value
* Exclusions via -...
Expand Down

0 comments on commit 254dd31

Please sign in to comment.