使环境变量可从公式访问

时间:2019-01-15 16:58:22

标签: ruby environment-variables homebrew

由于本文https://wheniwork.engineering/creating-a-private-homebrew-tap-with-gitlab-8800c453d893

,我尝试为gitlab写一个私人自制的水龙头

这是我的公式:

public class MainActivity extends AppCompatActivity {
Button btn_run;
Button btn_select;
TextView tv;
FrameLayout camera_preview;
Camera camera;
ShowCamera showCamera;
DrawView drawView;
Bitmap bitmap;
Canvas canvas;

float x_touch;
float y_touch;
int X_touch;
int Y_touch;
String sDown;

public boolean camera_state = true;
private boolean onSelect = false;
private boolean canSelect = false;
private boolean has_camera = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // элементы из activity_main
    btn_run = findViewById(R.id.btn_run);
    btn_select = findViewById(R.id.btn_selectStar);
    camera_preview = findViewById(R.id.camera_preview);
    tv = findViewById(R.id.tv);

    // проверка на наличие камеры
    if (!checkCameraHardware(MainActivity.this)) {
        has_camera = false;
        Toast toast = Toast.makeText(this, "Камера не найдена", Toast.LENGTH_LONG);
        toast.show();
    }

    // open camera
    camera = Camera.open();
    showCamera = new ShowCamera(this, camera);
    camera_preview.addView(showCamera);
    camera_preview.setVisibility(View.VISIBLE);
    addTouchListener();

}

public void btnRunOnClick(View view) {
    if (has_camera) {
        if (camera_state) {
            camera_state = false;
            btn_run.setText("Остановить");
            camera_preview.setVisibility(View.VISIBLE);
            Toast toast = Toast.makeText(this, "Камера остановлена", Toast.LENGTH_SHORT);
            toast.show();
            camera.startPreview();
        } else {
            camera_state = true;
            btn_run.setText("Запустить");
            camera_preview.setVisibility(View.VISIBLE);
            Toast toast = Toast.makeText(this, "Камера запущена", Toast.LENGTH_SHORT);
            toast.show();
            camera.stopPreview();
        }
    } else {
        Toast toast = Toast.makeText(this, "Камера не может быть запущена, \nпоскольку ее нет", Toast.LENGTH_LONG);
        toast.show();
    }

}

public void btnSelectStarOnClick(View view) {
    if (onSelect) {
        onSelect = false;
        canSelect = true;
        btn_select.setText("Прекратить");
        Toast toast = Toast.makeText(this, "Выберите объект", Toast.LENGTH_SHORT);
        toast.show();
    } else {
        onSelect = true;
        canSelect = false;
        btn_select.setText("Выбрать");
        Toast toast = Toast.makeText(this, "Выбор запрещен", Toast.LENGTH_SHORT);
        toast.show();
    }
}


private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        return true;
    } else {
        return false;
    }
}


private void addTouchListener(){
    camera_preview.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(canSelect){
                x_touch = event.getX();
                y_touch  = event.getY();
                X_touch = Math.round(x_touch);
                Y_touch = Math.round(y_touch);
                sDown = "Down: " + X_touch + "," + Y_touch;
                tv.setText(sDown);
            }
            return false;
        }
    });
}
}

我在环境中定义了class Pouet < Formula desc "Pouet command line utility" homepage "https://gitlab.mycompany.com/martin/Pouet" url "https://gitlab.mycompany.com/api/v4/projects/0000017/repository/archive?private_token=#{ENV['HOMEBREW_GITLAB_TOKEN']}" version "1.0" sha256 "ffdb029ada6d03fe3fa323577693cc233ef3d2300784a8d79077cecf6df7a470" def install system "echo", ENV["HOMEBREW_GITLAB_TOKEN"] end test do pouet --help end end ,因此可以从启动HOMEBREW_GITLAB_TOKEN命令的外壳程序中对其进行访问。

很遗憾,下载失败,因为brew进程中不知道HOMEBREW_GITLAB_TOKEN变量。

有什么主意吗?

2 个答案:

答案 0 :(得分:1)

自制软件具有多个级别的环境变量筛选。遇到的第一个问题与您的问题的先前版本有关,在该版本中您拥有变量GITLAB_HOMEBREW_TOKEN。 Homebrew的initial filtering仅允许specific environment variables传递给brew命令,以避免污染构建环境。因此,GITLAB_HOMEBREW_TOKEN不会通过,因为它不是以HOMEBREW_开头(或与其他任何列入白名单的变量匹配)。

第二级过滤是您当前要达到的过滤条件,如果它们与以下Ruby正则表达式匹配,则为“敏感”环境变量get filtered

/(cookie|key|token|password)/i

由于您的变量在变量名称中具有关键字TOKEN,因此在到达公式之前会被过滤掉。

简而言之,您需要使用以HOMEBREW_ 开头的其他环境变量名称,例如:

HOMEBREW_GITLAB_SECRET=secretsquirrel brew install Pouet

,您将可以在Formula的Ruby代码中访问此代码。

有关存在的环境变量过滤类型的背景,请参见https://docs.brew.sh/Formula-Cookbook#using-environment-variables

答案 1 :(得分:0)

从环境变量名称中删除$

system "echo", ENV["GITLAB_HOMEBREW_TOKEN"]

请参阅:

[1] pry(main)> ENV['LC_NAME']
=> "pl_PL.UTF-8"
[2] pry(main)> ENV['$LC_NAME']
=> nil