From be9c7cc6043475c4291c3eb16efb703fa2fbbbfc Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 27 Jul 2022 00:49:04 +0700 Subject: cmd/ini: a CLI to get and set values in the INI file format This is the CLI that implements the lib/ini for getting and setting the key's value from INI file. --- cmd/ini/main.go | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 cmd/ini/main.go diff --git a/cmd/ini/main.go b/cmd/ini/main.go new file mode 100644 index 00000000..87913799 --- /dev/null +++ b/cmd/ini/main.go @@ -0,0 +1,184 @@ +// Copyright 2022, Shulhan . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Command ini provide a command line interface to get and set values in the +// [INI file format]. +// +// [INI file format]: https://godocs.io/github.com/shuLhan/share/lib/ini +package main + +import ( + "flag" + "fmt" + "io" + "log" + "os" + "strings" + + "github.com/shuLhan/share" + "github.com/shuLhan/share/lib/ini" +) + +const usage = `= ini + +A command line interface to get and set values in the INI file format [1]. + +== SYNOPSIS + + ini [VALUE] + +== ARGUMENTS + +get + Print the key's value to the stdout. + If the key not found it will print empty line. + +help + Print the command usage. + +set + Changes the key's value in the INI file to VALUE. + +version + Print the current command version. + + + Tag is combination of section, subsection, and key string separated + by colon ":", using the following format, + + SECTION ":" SUBSECTION ":" KEY ":" DEFAULT + + At least one of the tag should be defined. + + + New value for key when calling set. + + + Path to the INI file. + If set to "-" and the command is "get", it will read the INI content + from standard input. + +== EXAMPLES + +Get key's value, + + $ ini get "user::name" lib/ini/testdata/input.ini + Shulhan + + $ cat lib/ini/testdata/input.ini | ini get "user::name" - + Shulhan + +Set key's value, + + $ ini set "user::name" "my name" lib/ini/testdata/input.ini + + $ ini get "user::name" lib/ini/testdata/input.ini + my name + +== REFERENCES + +[1] https://godocs.io/github.com/shuLhan/share/lib/ini` + +const ( + cmdGet = "get" + cmdHelp = "help" + cmdSet = "set" + cmdVersion = "version" +) + +func main() { + log.SetFlags(0) + log.SetPrefix("ini: ") + + flag.Parse() + + var ( + args = flag.Args() + + cmd string + ) + + if len(args) == 0 { + log.Fatal(usage) + } + + cmd = strings.ToLower(args[0]) + + switch cmd { + case cmdGet: + doGet(args[1:]) + + case cmdHelp: + fmt.Println(usage) + + case cmdSet: + doSet(args[1:]) + + case cmdVersion: + fmt.Println("ini v" + share.Version) + + default: + log.Println("ini: unknown command:", cmd) + } +} + +func doGet(args []string) { + var ( + cfg *ini.Ini + tags []string + raw []byte + vstr string + err error + ) + + if len(args) < 2 { + log.Fatalf("%s: missing arguments", cmdGet) + } + + tags = ini.ParseTag(args[0]) + + vstr = args[1] + if vstr == "-" { + raw, err = io.ReadAll(os.Stdin) + if err != nil { + log.Fatalf("%s: %s", cmdGet, err) + } + + cfg, err = ini.Parse(raw) + } else { + cfg, err = ini.Open(vstr) + } + if err != nil { + log.Fatalf("%s: %s", cmdGet, err) + } + + vstr, _ = cfg.Get(tags[0], tags[1], tags[2], tags[3]) + fmt.Println(vstr) +} + +func doSet(args []string) { + var ( + cfg *ini.Ini + tags []string + err error + ) + + if len(args) < 3 { + log.Fatalf("%s: missing arguments", cmdSet) + } + + tags = ini.ParseTag(args[0]) + + cfg, err = ini.Open(args[2]) + if err != nil { + log.Fatalf("%s: %s", cmdSet, err) + } + + cfg.Set(tags[0], tags[1], tags[2], args[1]) + + err = cfg.Save(args[2]) + if err != nil { + log.Fatalf("%s: %s", cmdSet, err) + } +} -- cgit v1.3