Skip to content

Commit

Permalink
snap: tweak commands
Browse files Browse the repository at this point in the history
This adds a new (hidden) `snap blame` command reserved for future
use.
  • Loading branch information
mvo5 committed Sep 20, 2018
1 parent d7c33bf commit 7b74573
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cmd/snap/cmd_blame.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2018 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package main

import (
"fmt"
"math/rand"

"github.com/jessevdk/go-flags"
)

type cmdBlame struct{}

var authors []string

func init() {
cmd := addCommand("blame",
"",
"",
func() flags.Commander {
return &cmdBlame{}
}, nil, nil)
cmd.hidden = true
}

func (x *cmdBlame) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
if len(authors) == 0 {
return nil
}

fmt.Fprintf(Stdout, "It's all %s's fault.\n", authors[rand.Intn(len(authors))])
return nil
}
7 changes: 7 additions & 0 deletions cmd/snap/cmd_blame_generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

// generated by mkauthors.sh; do not edit

func init() {
authors = []string{"Mark Shuttleworth", "Gustavo Niemeyer", "Sergio Schvezov", "Simon Fels", "Kyle Fazzari", "Sergio Cazzolato", "Leo Arias", "Gustavo Niemeyer", "Federico Gimenez", "Maciej Borzecki", "Jamie Strandboge", "Pawel Stolowski", "John R. Lenton", "Samuele Pedroni", "Zygmunt Krynicki", "Michael Vogt",}
}
59 changes: 59 additions & 0 deletions mkauthors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash
set -e

# debugging if anything fails is tricky as dh-golang eats up all output
# uncomment the lines below to get a useful trace if you have to touch
# this again (my advice is: DON'T)
#set -x
#logfile=/tmp/mkauthors.log
#exec >> $logfile 2>&1
#echo "env: $(set)"
#echo "mkauthors.sh run from: $0"
#echo "pwd: $(pwd)"

# we have two directories we need to care about:
# - our toplevel pkg builddir which is where "mkauthors.sh" is located
# and where "snap-confine" expects its cmd/VERSION file
# - the GO_GENERATE_BUILDDIR which may be the toplevel pkg dir. but
# during "dpkg-buildpackage" it will become a different _build/ dir
# that dh-golang creates and that only contains a subset of the
# files of the toplevel buildir.
PKG_BUILDDIR=$(dirname "$0")
GO_GENERATE_BUILDDIR="$(pwd)"

# run from "go generate" adjust path
if [ "$GOPACKAGE" = "cmd" ]; then
GO_GENERATE_BUILDDIR="$(pwd)/.."
fi

# Let's try to derive authors from git
if ! command -v git >/dev/null; then
exit
fi

# see if we are in a git branch, if not, do nothing
if [ ! -d .git ]; then
exit
fi

# "John Lenton" and "John R. Lenton" are the same guy
raw_authors="$(git shortlog -s|sort -n|grep -v "John Lenton"|tail -n14|cut -f2)"
authors=""
while read -r author; do
if [ "$author" = "sergio-j-cazzolato" ]; then
author="Sergio Cazzolato"
fi
authors="$authors \"$author\","
done <<< "$raw_authors"


cat <<EOF > "$GO_GENERATE_BUILDDIR/cmd/snap/cmd_blame_generated.go"
package main
// generated by mkauthors.sh; do not edit
func init() {
authors = []string{"Mark Shuttleworth", "Gustavo Niemeyer", $authors}
}
EOF

0 comments on commit 7b74573

Please sign in to comment.