Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuheiKubota committed Mar 31, 2024
0 parents commit a19c4ac
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: release
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.22'
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
46 changes: 46 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
project_name: eksemel
env:
- GO111MODULE=on
before:
hooks:
- go mod tidy
builds:
- binary: eksemel
flags:
- -trimpath
ldflags:
- -s -w
- -X main.Version={{.Version}}
- -X main.Revision={{.ShortCommit}}
env:
- CGO_ENABLED=0
goos:
- windows
- darwin
- linux
goarch:
- amd64
- 386
hooks:
post: upx --lzma '{{ .Path }}'
archives:
- name_template: '{{ .ProjectName }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
format_overrides:
- goos: windows
format: zip
release:
prerelease: auto
changelog:
skip: false
filters:
exclude:
- '^test:'
groups:
- title: 'Features'
regexp: "^.*feat[(\\w)!]*:+.*$"
order: 0
- title: 'Bug fixes'
regexp: "^.*fix[(\\w)!]*:+.*$"
order: 1
- title: 'Others'
order: 999
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Shuhei Kubota

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.
20 changes: 20 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module github.com/shu-go/eksemel

go 1.22.1

require (
github.com/antchfx/xmlquery v1.3.18
github.com/go-xmlfmt/xmlfmt v1.1.2
github.com/shu-go/gli/v2 v2.0.1
)

require (
github.com/antchfx/xpath v1.2.5 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/shu-go/cliparser v0.2.2 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
53 changes: 53 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
github.com/antchfx/xmlquery v1.3.18 h1:FSQ3wMuphnPPGJOFhvc+cRQ2CT/rUj4cyQXkJcjOwz0=
github.com/antchfx/xmlquery v1.3.18/go.mod h1:Afkq4JIeXut75taLSuI31ISJ/zeq+3jG7TunF7noreA=
github.com/antchfx/xpath v1.2.4/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/antchfx/xpath v1.2.5 h1:hqZ+wtQ+KIOV/S3bGZcIhpgYC26um2bZYP2KVGcR7VY=
github.com/antchfx/xpath v1.2.5/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/go-xmlfmt/xmlfmt v1.1.2 h1:Nea7b4icn8s57fTx1M5AI4qQT5HEM3rVUO8MuE6g80U=
github.com/go-xmlfmt/xmlfmt v1.1.2/go.mod h1:aUCEOzzezBEjDBbFBoSiya/gduyIiWYRP6CnSFIV8AM=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/shu-go/cliparser v0.2.2 h1:EONHyGHC+9l2mukTBNsxvngXro73T22kXb+agVFd0CY=
github.com/shu-go/cliparser v0.2.2/go.mod h1:oX+xgwUi9B2OzBFudKc5ayoqWhlVAbuN67rAqyWvvQ4=
github.com/shu-go/gli/v2 v2.0.1 h1:XA1QSfUdpWW1FIqLjuXo+Gs/IjEy+uviuxKQ45IRYC8=
github.com/shu-go/gli/v2 v2.0.1/go.mod h1:pSC0XKb69hEarQpiQU74CNiKpcS7a90iJAMUGL3SinQ=
github.com/shu-go/gotwant v0.0.0-20190920074605-b4f19c0bac91 h1:nwDc3kHbf9scf1UZIWiWw5tZF3Z4yOJAMjNN+kYXJwE=
github.com/shu-go/gotwant v0.0.0-20190920074605-b4f19c0bac91/go.mod h1:FZepfqvib0mXjHiaQPTv0RUD5QMpMA/FHLfBQjZRRQg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
193 changes: 193 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package main

import (
"errors"
"fmt"
"os"
"strings"

"github.com/antchfx/xmlquery"
"github.com/go-xmlfmt/xmlfmt"
"github.com/shu-go/gli/v2"
)

type globalCmd struct {
Replace replaceCmd
Delete deleteCmd
Add addCmd
}

type replaceCmd struct {
_ struct{} `help:"--xpath //* --value newvalue hoge.xml"`

XPath string `cli:"xpath" required:"true"`
Value string `cli:"value" required:"true"`
}

func (c replaceCmd) Run(args []string) error {
if len(args) == 0 {
return errors.New("input required")
}

f, err := os.Open(args[0])
if err != nil {
return err
}
doc, err := xmlquery.Parse(f)
if err != nil {
return err
}

nodes, err := xmlquery.QueryAll(doc, c.XPath)
if err != nil {
return err
}
for _, n := range nodes {
if n.Type == xmlquery.AttributeNode {
n.Parent.SetAttr(n.Data, c.Value)
} else {
n.Data = c.Value
}
}

fmt.Println(strings.TrimSpace(xmlfmt.FormatXML(doc.OutputXML(true), "", " ", true)))

return nil
}

type deleteCmd struct {
_ struct{} `help:"--xpath //* hoge.xml"`

XPath string `cli:"xpath" required:"true"`
}

func (c deleteCmd) Run(args []string) error {
if len(args) == 0 {
return errors.New("input required")
}

f, err := os.Open(args[0])
if err != nil {
return err
}
doc, err := xmlquery.Parse(f)
if err != nil {
return err
}

nodes, err := xmlquery.QueryAll(doc, c.XPath)
if err != nil {
return err
}
for _, n := range nodes {
if n.Type == xmlquery.AttributeNode {
n.Parent.RemoveAttr(n.Data)
} else {
xmlquery.RemoveFromTree(n)
}
}

fmt.Println(strings.TrimSpace(xmlfmt.FormatXML(doc.OutputXML(true), "", " ", true)))

return nil
}

type addCmd struct {
_ struct{} `help:"--xpath //* --name newnode --value newvalue hoge.xml"`

XPath string `cli:"xpath" help:"parent" required:"true"`
Name string `cli:"name" required:"true" help:"nodename, @attrname, #text, #cdata-section, #comment"`
Value string `cli:"value" help:"if --name is a nodename, --value is its text"`
Sibling bool `cli:"sibling" default:"false" help:"as a LAST sibling"`
}

func (c addCmd) Run(args []string) error {
if len(args) == 0 {
return errors.New("input required")
}

f, err := os.Open(args[0])
if err != nil {
return err
}
doc, err := xmlquery.Parse(f)
if err != nil {
return err
}

nodes, err := xmlquery.QueryAll(doc, c.XPath)
if err != nil {
return err
}

addfunc := xmlquery.AddChild
if c.Sibling {
addfunc = xmlquery.AddSibling
}

for _, n := range nodes {
if strings.HasPrefix(c.Name, "@") {
xmlquery.AddAttr(n, c.Name[1:], c.Value)

} else if c.Name == "#text" {
addfunc(n, &xmlquery.Node{
Type: xmlquery.TextNode,
Data: c.Value,
})

} else if c.Name == "#cdata-section" {
nn := &xmlquery.Node{
Type: xmlquery.CharDataNode,
Data: c.Value,
}
addfunc(nn, &xmlquery.Node{
Type: xmlquery.TextNode,
Data: c.Value,
})
xmlquery.AddChild(n, nn)

} else if c.Name == "#comment" {
nn := &xmlquery.Node{
Type: xmlquery.CommentNode,
Data: c.Value,
}
//xmlquery.AddChild(nn, &xmlquery.Node{
// Type: xmlquery.TextNode,
// Data: c.Value,
//})
addfunc(n, nn)

} else {
nn := &xmlquery.Node{
Data: c.Name,
}
if c.Value != "" {
xmlquery.AddChild(nn, &xmlquery.Node{
Type: xmlquery.TextNode,
Data: c.Value,
})
}
addfunc(n, nn)

}
}

fmt.Println(strings.TrimSpace(xmlfmt.FormatXML(doc.OutputXML(true), "", " ", true)))

return nil
}

// Version is app version
var Version string

func main() {
app := gli.NewWith(&globalCmd{})
app.Name = "eksemel"
app.Desc = "An XML file manipulator"
app.Version = Version
app.Usage = ``
app.Copyright = "(C) 2024 Shuhei Kubota"
if err := app.Run(os.Args); err != nil {
os.Exit(1)
}
}

0 comments on commit a19c4ac

Please sign in to comment.