LoginSignup
9
9

More than 5 years have passed since last update.

ただ Desktop Notification するだけのコマンドを作った話

Last updated at Posted at 2017-12-23

ある日

PS> yarn build

yarn run v1.3.2
$ yarn install
[1/4] Resolving packages...
success Already up-to-date.
$ webpack --config webpack.config.js
...

...

...

...

( 長いな。ちょっとインスタでも更新しようか。 )
ブラウザに移動。

...

[ 20分経過 ]

... あ、ビルドしてたの忘れてた。

が多いので、終わったら通知したかった。
ちなみにインスタはやったことない。

デスクトップ通知

調べてみると、案外あった。

  • Linux
    • notify-send
  • Mac
  • Windows
    • growl for windows + growlnotify

クロスプラットフォームでは、node-notifier などがある。

でも、クロスプラットフォームかつワンバイナリなやつ が欲しい。

Hi

それで、Hi というコマンドを作った。

kentork/hi

クロスプラットフォームと言いながら、 Circle Ci のビルドでコケているので手動で上げた Windows バイナリしかない。

開発

  • Go 1.9

初めは、これを使おうと思った が、外部ツールに依存しているっぽくて動かなかった。

なので、こっちを使った。標準の通知を使えるので出せた。

実際に作ったものは、これを 薄ぅーく ラップしただけのもの。

main.go
package main

import (
    "os"
    "runtime"

    "github.com/martinlindhe/notify"
    "github.com/urfave/cli"
)

func main() {
    app := cli.NewApp()

    app.Action = func(c *cli.Context) error {
        appname := "hi"
        if runtime.GOOS == "windows" {
            // 理由は後述
            appname = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\WindowsPowerShell\\v1.0\\powershell.exe"
        }

        title := "hi"
        contents := "there"

        if len(c.Args()) >= 2 {
            title = c.Args().Get(0)
            contents = c.Args().Get(1)
        } else if len(c.Args()) >= 1 {
            title = "hi there"
            contents = c.Args().Get(0)
        }

        notify.Notify(appname, title, contents, "dorobou_jikan.png")
        return nil
    }

    app.Name = "hi"
    app.Version = "0.1.0"
    app.Usage = "desktop notifier"
    app.Run(os.Args)
}
hi [title] [contents]

● i.e. ビルドを待って通知

go build; hi go complete

hi.gif

出た。
あれ? 何か表示がおかしい…

Fall Creators Update

数ヶ月前に Windows に適用された Fall Creators Update で、どうやら Notification API に大きな変更が入ったらしい。

No Notification is shown
Notification API do not work with Windows 10 16299.19 (fall creators update)
Fall Creators Updateを適用するとトースト通知できなくなる

原因は、AppID を渡さないといけないとのことで、それには対応しているんだけど、タイトルやコンテンツが上手く書き換わらなかった。

もう少し触ってみて、どうしようもなかったら独自で実装しよう。

もっとやりたい事

  • 特定コマンドでの監視から、状態変化後に通知
    • docker run -d postgres とかやると、コマンドは終わってるけど内部でデータ復元とかしてて、アクセスできなかったりする
    • webpack --watch なんかでビルド終わったことを、ファイル更新時間の監視とかで通知して欲しい
      • webpack-build-notifier みたいなのではなく、汎用的に
9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9